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:
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.