-
Notifications
You must be signed in to change notification settings - Fork 149
Done adding Catch with return of Promise #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Done adding Catch with return of Promise #77
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks like a useful addition but won't work in its current state and needs tests written for both the new functions before we can merge.
Promise.cs
Outdated
@@ -51,6 +51,8 @@ public interface IPromise<PromisedT> | |||
/// </summary> | |||
IPromise<PromisedT> Catch(Func<Exception, PromisedT> onRejected); | |||
|
|||
IPromise<ConvertedT> Catch<ConvertedT>(Func<Exception, IPromise<ConvertedT>> onRejected); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs the <summary>...</summary>
comment on it so that a description of how the method works shows up in IntelliSense.
This should also return IPromise<PromisedT>
, not IPromise<ConvertedT>
. This is because Catch
doesn't always run, so if we try to convert the type of the promise in the catch handler, we have no way of knowing after the catch whether the type is the original PromisedT or the ConvertedT.
promiseReturningAnInt()
.Then<int>(value => value + 1)
.Catch<string>(ex => Promise.Resolved<string>(ex.Message))
.Then<???>(arg => /* no way to know whether arg should be an int or a string */);
The only case where you can return a different type of promise in the rejected handler is in the overload of .Then
where both the resolved and rejected handlers are guarenteed to return the same type - see Promise.cs line 75
Promise_NonGeneric.cs
Outdated
@@ -46,6 +46,8 @@ public interface IPromise | |||
/// </summary> | |||
IPromise Catch(Action<Exception> onRejected); | |||
|
|||
IPromise<ConvertedT> Catch<ConvertedT>(Func<Exception, IPromise<ConvertedT>> onRejected); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
I made it like this for my use. But I didn't know how to do it correctly, that's why I made a pull request. Maybe you can help me doing this |
I made a change in my code, the Catch can retrun a promise of the same type of incoming promise. But there is a issue in one of your test that fail the build. I don't know how you want to handle it. |
…e into CatchImprovement
Hi @RoryDungan! |
int excepectedValue = -1; | ||
int actualValue = 0; | ||
|
||
promise.Catch(err => Promise<int>.Rejected(new Exception())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since promise is resolved (promise.Resolve(1)
further down), this Catch
block is never executed.
int excepectedValue = 1; | ||
int actualValue = 0; | ||
|
||
promise.Catch(err => Promise<int>.Resolved(-1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the promise is resolved, this catch block is never executed.
.Then(result => { actualValue = result; }) | ||
.Catch(err => throw new Exception("Should not happend.")); | ||
|
||
promise.Resolve(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would make more sense here to make the first promise reject and verify that actualValue
was set to -1, to verify that the value from a promise returned inside a Catch
is coming through.
Hey, sorry letting this branch go stale for so long. Reading through the A+ guide it I agree it would seem like this is needed. I just tested and this functionality does work in standard JS promises so would definitely be good to have. I think in terms of getting this ready to merge again, there are a few things that would need to be done:
|
I've started to work on it a bit in my own fork, but I have a judgment call to make and I wanted to ask you for your opinion. If we add an overload for
can no longer be resolved, since - because the return type needs to be implied since there is no return in the throw-only body - both OTOH, I could add it as a method with a signature like How do you feel about this? (Edit: the name |
… to allow asynchronous continuation These versions of Catch allow for recovery of a chain of promises by handling an error from an earlier state, then getting back into the "normal" flow of processing. Rework of PR Real-Serious-Games#77.
… to allow asynchronous continuation These versions of Catch allow for recovery of a chain of promises by handling an error from an earlier state, then getting back into the "normal" flow of processing. Rework of PR Real-Serious-Games#77.
… to allow asynchronous continuation These versions of Catch allow for recovery of a chain of promises by handling an error from an earlier state, then getting back into the "normal" flow of processing. Rework of PR Real-Serious-Games#77.
… to allow asynchronous continuation These versions of Catch allow for recovery of a chain of promises by handling an error from an earlier state, then getting back into the "normal" flow of processing. Rework of PR Real-Serious-Games#77.
Since (I think) I have no way of updating this pull request, see #109 for a new version of the code that should be mergeable and fixes the aforementioned issues. |
No description provided.