I encountered some issue in Bluebird/Promises. For the Promise1 everything works fine either if call fullfill or reject. However when we return Promise2 in a finally block it works only for reject and for fullfil we get undefined in then's callback.
function getPromise1() {
return new Promise(function(fulfill, reject) {
fulfill("OK1");
});
}
function getPromise2() {
return new Promise(function(fulfill, reject) {
fulfill("OK2");
});
}
getPromise1()
.then(function(c){
console.log(c);
})
.catch(function(e) {
console.log(e);
})
.finally(function() {
return getPromise2();
})
.then(function(c){
console.log(c);
})
.catch(function(e) {
console.log(e);
});
Output:
OK1
undefined
The finally
block does not change the return value.
There are special semantics for .finally() in that the final value cannot be modified from the handler.
Bluebird will wait for it, but it will not change the return value (this is an opinionated choice and aligns with the proposed ECMAScript standard semantics - like finally
in some languages and unlike others).
If you want to chain a handler irrespective of the previous promise's outcome, you can use .reflect() to convert the result to a PromiseInspection.
The official documentation is here, although it doesn't really make this use-case very clear at the time of this writing.
Better example:
Promise.resolve("OK1")
.then(function(x) {
console.log(x); // outputs OK1
return Promise.reject("Rejection demo");
})
.reflect()
.then(function(settled) {
if (settled.isRejected()) {
// outputs Rejected: Rejection demo
console.log("Rejected:", settled.reason());
}
if (settled.isFulfilled()) {
console.log("Fulfilled:", settled.value()); // skipped
}
return Promise.resolve("OK2");
})
.then(function(c){
console.log(c); // outputs OK2
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With