What is Bluebird JS? Bluebird JS is a fully-featured Promise library for JavaScript. The strongest feature of Bluebird is that it allows you to “promisify” other Node modules in order to use them asynchronously. Promisify is a concept applied to callback functions.
A Promise represents something that is eventually fulfilled. A Promise can either be rejected or resolved based on the operation outcome. ES6 Promise is the easiest way to work with asynchronous programming in JavaScript.
Promises are a new feature in the ES6 (ES2015) JavaScript spec that allow you to very easily deal with asynchronous code without resolving to multiple levels of callback functions. Goodbye callback hell!
As of February 7, 2018
Chrome 63+, Firefox 58+, and Opera 50+ support Promise.finally
.
In Node.js 8.1.4+ (V8 5.8+), the feature is available behind the flag --harmony-promise-finally
.
The Promise.prototype.finally ECMAScript Proposal is currently in stage 3 of the TC39 process.
In the meantime to have promise.finally functionality in all browsers; you can add an additional then()
after the catch()
to always invoke that callback.
Example:
myES6Promise.then(() => console.log('Resolved'))
.catch(() => console.log('Failed'))
.then(() => console.log('Always run this'));
JSFiddle Demo: https://jsfiddle.net/9frfjcsg/
Or you can extend the prototype to include a finally()
method (not recommended):
Promise.prototype.finally = function(cb) {
const res = () => this;
const fin = () => Promise.resolve(cb()).then(res);
return this.then(fin, fin);
};
JSFiddle Demo: https://jsfiddle.net/c67a6ss0/1/
There's also the Promise.prototype.finally shim library.
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