Thursday, February 20, 2020
Home Javascript How to use BlueBird Promise Library in Node.js

How to use BlueBird Promise Library in Node.js

Author

Date

Category

Bluebird 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. We use Promisify concept to ensure that every callback function returns some sort of value. The library is quite helpful in dealing Callback Hell.

So if a Node JS module contains a callback function which does not return a value, if we Promisify the node module, all the function’s in that specific node module would automatically be modified to ensure that it returns a value.

Generating promises with the BlueBird library

Step 1: Installing the NPM Modules

To use Bluebird from within a Node application, we need the Bluebird module at first. To install the Bluebird module, run the below command

npm install bluebird

Step 2: Including the BlueBird module in the project

The next step is to include the bluebird module in your code so that we can promisify the custom function we have.

Step 3: Promisify the function

The next step is to use the promisify feature of the BlueBird module to convert a normal Node.js callback function to return a promise. The code would look something like this:

Step 4: Use the new Function

Once you are done with the promisify function, you can use it as a normal Promise. Let us see how the entire code would look like:

Conclusion

Once you finis the above example, you are almost sure about how to use BlueBird library in Node.js to convert a Node.js style Callback Function into a Promise. We are coming up with some tutorials, that can explain how can you use BlueBird to Promisify all the content or a Node.js module. Till then, stay tune with us.

Happy Coding!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

Snehasish Nayak
I am Snehasish Nayak, a Senior Programmer at NIC, Bhubaneswar and a Google Product Expert.

Recent posts

Recent comments