RXPromise
RXPromise copied to clipboard
Proposol: API Additions
promise.catch
When the catch method is called with argument onRejected the following steps are taken:
- Let promise be the this value.
- Return Invoke(promise,
"then", (undefined, onRejected)).
promise.finally
not part of the spec yet, was postponed to ES7.
It is useful for ensuring a callback is run regardless of the promise it chains's fate. Also it's returned promise is fated to the promise it was chained from and as such as no ability to effect downstream outcomes.
In practice this is useful for ensuring cleanup regardless of outcome.
RXPromise *promise = [RXPromise resolveWith: 1]
promise.finally(^{
// void, no possible way to effect downstream fate
}).then(^((int) value){
// value == 1
}, nil)
I appreciate the idea having an API like this (pseudo code):
typedef id (^successHandler_t)(id result); typedef id (^errorHandler_t)(NSError* result);
- (Promise*) then(successHandler_t);
- (Promise*) catch(errorHandler_t);
The addition with finally certainly requires investigation and a clear definition what actually shall happen.
Should finally create a separate "Continuation" with a finally handler as its sole handler? Otherwise, it may become more complex, if the Continuation had also a success and an error handler.
There's this question, which is not that clear to me:
- What does the expression
promise.finally(^{});return?
Should finally possibly return the receiver?
In your example it obviously returns a promise.
Finally returns the receiver. It only has a single callback and that callback has an arity of 0. The callbacks return value is ignored.
My related tests in RSVP.js
https://github.com/tildeio/rsvp.js/blob/master/test/tests/extension-test.js#L1836
Some extra tests exist to ensure finally works correctly with I inheritance.