I am trying to get my head around Promises/A+ and I am confused on a couple of points. Consider this:
function foo() {
console.log("In 'foo' function: --> which returns a Promise");
return new Promise(function (resolve, reject) {
resolve()
});
}
var promise1 = foo();
var promise2 = promise1.then(function () {
console.log("Promise 1 has been resolved --> onFulfilled() returns a value");
return 1;
});
var promise3 = promise2.then(function () {
console.log("Promise 2 has been resolved --> onFulfilled() is not returning anything from inFulfilled()");
});
var promise4 = promise3.then(function () {
console.log("Promise 3 has been resolved");
});
/* Console:
In 'foo' function: --> which returns a Promise
Promise 1 has been resolved --> onFulfilled() returns a value
Promise 2 has been resolved --> onFulfilled() is not returning anything from inFulfilled()
Promise 3 has been resolved
*/
resolve()
, otherwise the execution halts. Why is it that, with Promises 2 and 3 the execution considers these resolved (without requiring resolve()
) and progresses to the next .then
in the chain?Promise handlers (success or error handler) have a return value. Each then call creates a new promise.
If a value is returned, then the promise is auto-resolved. If another promise is returned, then the promise will wait until it is either resolved or rejected before continuing the next then call in the promise chain.
I think this explains the behaviour you are seeing.