-
Notifications
You must be signed in to change notification settings - Fork 23
Open
Labels
Description
I understand this is objective-c land, but as promises in JavaScript are now part of a formal standard, is there interest in aligning this project with those semantics?
One very specific and confusing difference is how the promises compose.
in ES6: there is no concept of a promise fulfilling with another promise, when a promise is to resolve another promise, it merely assimilates the new promises state.
Simple example:
var rejectedPromise = Promise.reject(new Error);
var promise = Promise.resolve(rejectedPromise)
promise.then(function(){
// will not invoke
}).catch(reason) {
// will invoke, with the reason of the assimilated promise
// to propagate the rejection
throw reason;
// return to "catch" and handle the rejection, providing a new success value"
return 1;
// return a promise, to "retry"
return User.find(1);
}); Why is this useful?
It encourages encapsulation, and mimics synchronous programmings try/catch
example continued:
sync:
function fetch() {
var something = ajax('/something');
var somethingElse = ajax('/something-else/' + something.id);
// if either attempt throws, fetch throws
return something;
}async
function fetch() {
return ajax('/something').then(function(value) {
return ajax('/something-else/' + value.id);
});
}
// if either attempt rejects, the promise returned from fetch rejects
// refactor further
function fetchSomethingElse(value) {
return ajax('/something-else/' + value.id);
}
function fetch() {
return ajax('/something').then(fetchSomthingElse);
}
// identical semantics.To summarize, these chaining and assimilation semantics encourage best practices. Although this library is clearly not JavaScript, aligning only improves mindshare on the topic.