-
Notifications
You must be signed in to change notification settings - Fork 149
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
Applying the "try-catch-finally"/"using" patterns #9
Comments
This an interesting discussion. Can I just clarify one thing... it seems to me that in this case you could just do this:
|
For your issue with Catch, can you just do this:
|
I'm interested in your ideas for the Finally and Using functions. Can you please make up some examples of use? |
In the specific case that I stated yes, thats true, I could just encapsulate it into a using block. But let's assume that we're really resolving the promises asynchronously (Actually it is really important that promises get always resolved asynchronously, one would need a "platform" layer for that, but that's a different topic) then the Stream would be disposed before the resolve handler is called which uses the Stream. One use case for "Finally" would be to e.g. unlock the UI after a asynchronous operation:
The "Using" would just be some syntactical sugar on top of the try-catch-finally-API of the promise but would clean up boilerplate code:
|
I love both of these ideas. Please feel free to contribute and continue the conversation here. If you are adding features we just ask that you keep the existing tests working and add unit-tests for any new features (ideally via TDD). |
Is the finally pattern implemented yet? We have something that needs to be done whether a chain of promises succeeds or fails. It's pretty standard in Promise implementations. Here's the example from bluebird: |
We really want the finally feature but have little time to implement it at the moment. Please feel free to fork and contribute. |
Implemented as described in issue Real-Serious-Games#9. It's just a wrapper around existing functionality.
I started using Promises to model asynchronous/pipelined execution flows in our game and I'm really impressed, it just works as I would expect! As I use it e.g. with I/O, I'd like to start a discussion on how to extend the library to work better with C# constructs like "using" and "try-catch-finally".
Try/Catch works (but the Catch method is violating A+ as it cannot return a value/promise) but I struggle to create a good "finally" case. Consider this:
How would
Store()
dispose of the MemoryStream when an exception occurs? AThen()
callback would not suffice, it would work when the preceding promise resolves, but when it rejects it won't. The disposing code could be duplicated and added asThen()
andCatch()
callbacks, but by specifying a rejection callback at this point, the exception will not be passed to the client-side error handling. I could explicitely re-throw the exception in aCatch()
callback, but that would not preserve the exception per se. The only way I could think of would be to duplicate the disposal code and return another rejecting promise with the same exception, which is currently not possible because theCatch()
callbacks can not return a promise (violating the A+ spec):What do you think about this? I'll maybe try to fork and fix the
Catch()
handling myself, but I'd also like to get some feedback on this issue first. Also I'd like to know if there are any other suggestions howfinally
can be supported more fluently and maybe even howusing
could be supported in any way!Suggestion for
finally
:IPromise Promise.Finally(Func<IPromise> callback)
instance methodFinally
callback can return a new promiseFinally
itself returns a promiseSuggestion for
using
:IPromise Promise.Using(IDisposable, Func<IPromise>)
Finally()
interfaceBasically implemented like this:
I'd love to see this library going forward, as we won't get 4.5 features in unity in the near future and this is a very good alternative!
The text was updated successfully, but these errors were encountered: