Real-Serious-Games / C-Sharp-Promise

Promises library for C# for management of asynchronous operations.
MIT License
1.2k stars 149 forks source link

Catch from a Promise<object> returns IPromise instead of IPromise<object> #94

Closed AllFatherGray closed 5 years ago

AllFatherGray commented 5 years ago

Old Code: `
public override IPromise Access()

{
    return new Promise<object>((resolve, reject) => 
    {
        StartCoroutine(Access(resolve: resolve, reject: reject));
    })
    .Catch(error => Debug.LogError(error));
}

`

After moving from 2.6 to 3.00 the compiler reports an error stating that Catch is returning IPromise instead of IPromise like in 2.6.

Have I missed something or is this a bug?

RoryDungan commented 5 years ago

This is intended behaviour. From version 3.0, you can return a value from a .Catch to be used in a later then:

var defaultValue = "foo";

FunctionReturningPromise()
    .Catch(ex => {
        Console.Error(ex.Message);
        return defaultValue;
    })
    .Then(v => Console.Log(v));

This means that, like JavaScript promises, if your .Catch handler doesn't return a value, no value will be passed through and you will end up with a non-value promise (IPromise as opposed to IPromise<T>).

To work around this, you should either return a default value in your .Catch handler or move the error handling outside of the function returning the promise.

AllFatherGray commented 5 years ago

Thanks for the response. The new behavior is intended so this issue is closed.