Using Bluebird Promises
In this post, we will give you information about Using Bluebird Promises. Here we will give you detail about Using Bluebird Promisesv And how to use it also give you a demo for it if it is necessary.
Bluebird is a popular promises library for JavaScript. It is a drop-in replacement for native Promises in JavaScript.
global.Promise = require('bluebird');
// Prints "42"
Promise.resolve(42).then(val => console.log(val));
Why do people use Bluebird as opposed to native Promises? There are 2 reasons:
1. Performance for Using Bluebird Promises:
Early native promise implementations were slow – the below benchmark script shows that creating a native promise is 3x slower than creating a Bluebird promise in Node.js 8:
// global.Promise = require('bluebird');
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite();
// add tests
suite.
add('new promise', function() {
return new Promise((resolve, reject) => {});
}).
on('cycle', function(event) {
console.log(String(event.target));
}).
on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
}).
run();
Below is the output, first with Bluebird and then with native promises:
$ ~/Workspace/libs/node-v8.17.0-linux-x64/bin/node ./bluebird.js
new promise x 36,846,162 ops/sec ±0.66% (95 runs sampled)
Fastest is new promise
$
$ ~/Workspace/libs/node-v8.17.0-linux-x64/bin/node ./bluebird.js
new promise x 12,244,609 ops/sec ±1.80% (84 runs sampled)
Fastest is new promise
However, in Node.js 12.x native promises are significantly faster than Bluebird.
2. Long Stack Traces for Using Bluebird Promises
Bluebird has built-in support for async stack traces. For example, the below script will not print the line where fn()
was called:
Promise.resolve().
then(fn).
catch(err => console.log(err));
function fn() {
return new Promise((resolve, reject) => {
setImmediate(() => reject(new Error('Oops')));
});
}
You get the below output:
$ node ./test
Error: Oops
at Immediate.setImmediate [as _onImmediate] (/app/test.js:8:31)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
But with Bluebird, you can enable long stack traces as shown below.
global.Promise = require('bluebird');
global.Promise.config({ longStackTraces: true });
Promise.resolve().
then(fn).
catch(err => console.log(err));
function fn() {
return new Promise((resolve, reject) => {
setImmediate(() => reject(new Error('Oops')));
});
}
Running the above script gives you the below stack trace, which includes the line number on which fn()
was called:
$ node ./test
Error: Oops
at Immediate.setImmediate [as _onImmediate] (/app/test.js:10:31)
From previous event:
at fn (/app/test.js:9:10)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
From previous event:
at Object.<anonymous> (/app/test.js:5:3)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Integration With Async/Await
Unfortunately there is no way to get async functions to return Bluebird promises. Even if you set global.Promise = require('bluebird');
, async functions will still return native promises.
const NativePromise = global.Promise;
global.Promise = require('bluebird');
async function run() { return 'Hello, World'; }
const p = run();
p instanceof NativePromise; // true
p instanceof global.Promise; // false
Should You Use Bluebird or Native Promises?
The reality is that, in 2020, most JavaScript apps don’t get much benefit from using Bluebird. Bluebird no longer has a singificant performance advantage over native promises in Node.js and modern browsers – native promises are actually faster. However, Bluebird can be a great tool for ensuring you get consistent performance with older browsers or older versions of Node.js.
JavaScript Fundamentals for
JavaScript is a programming language that is used to create interactive web pages. It is a client-side scripting language, which means that it runs on the user’s browser. JavaScript can be used to add animation, interactivity, and functionality to web pages.
Here are some of the fundamentals of JavaScript for Using Bluebird Promises:
- Variables: Variables are used to store data. They are declared using the
var
keyword. - Data types: JavaScript has a variety of data types, including strings, numbers, booleans, objects, and arrays.
- Operators: Operators are used to perform operations on data.
- Control flow statements: Control flow statements allow you to control the order in which your code is executed.
- Functions: Functions are blocks of code that can be reused.
- Objects: Objects are used to store data in key-value pairs.
- Arrays: Arrays are used to store data in a sequential order.
- Events and event handlers: Events are notifications that are sent by the browser when something happens, such as when the user clicks on an element or moves the mouse over an element. Event handlers are functions that are called in response to an event.
- DOM manipulation: The Document Object Model (DOM) is a tree-like structure that represents the elements of a web page. JavaScript can be used to manipulate the DOM to change the appearance or behavior of a web page.
These are just some of the fundamentals of JavaScript. There are many other concepts that you can learn as you continue to develop your skills.
Here are some resources that you can use to learn more about JavaScript:
- Mozilla Developer Network (MDN): The MDN is a great resource for learning about JavaScript. It has a comprehensive reference guide, tutorials, and articles on a wide range of topics.
- W3Schools: W3Schools is another great resource for learning about JavaScript. It has interactive tutorials and quizzes that can help you learn the basics of the language.
- JavaScript.info: JavaScript.info is a website that provides in-depth tutorials on JavaScript. It also has a forum where you can ask questions and get help from other JavaScript developers.
I hope this helps to Using Bluebird Promises!
Here are some additional tips for learning JavaScript:
- Start with the basics: Before you start trying to build complex applications, it’s important to learn the basics of JavaScript. This includes things like variables, data types, operators, and control flow statements.
- Practice regularly: The best way to learn JavaScript is to practice regularly. Try to find some time each day to work on JavaScript projects.
- Get help from others: If you get stuck, don’t be afraid to ask for help from others. There are many online forums and communities where you can get help from other JavaScript developers.
With a little practice, you’ll be able to learn JavaScript and start building amazing web applications.
Hope this code and post will help you for implementing Using Bluebird Promises. if you need any help or any feedback give it in the comment section or if you have a good idea about this post you can give it comment section. Your comment will help us to help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting posts and code Keep reading our blogs