So if a Node JS module contains a callback function which does not return a

So If A Node Js Module Contains A Callback Function Which Does Not Return A

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

So you can use BlueBird to make the MongoDB module run asynchronously. This just adds another level of ease when writing Node.js applications.

We will look at an example of how to use the bluebird module.

Our example will first establish a connection to the "Employee collection" in the "EmployeeDB" database. If "then" connection is established, then it will get all of the records in the collection and display them in the console accordingly.

Step 1) Installing the NPM Modules

To use Bluebird from within a Node application, the Bluebird module is required. To install the Bluebird module, run the below command

npm install bluebird

Step 2) The next step is to include the bluebird module in your code and promisify the entire MongoDB module. By promisify, we mean that bluebird will ensure that each and every method defined in the MongoDB library returns a promise.


Code Explanation:-

  1. The require command is used to include the Bluebird library.
  2. Use Bluebirds .promisifyAll() method to create an async version of every method the MongoDB module provides. This ensures that each method of the MongoDB module will run in the background and ensure that a promise is returned for each method call in the MongoDB library.

Popular Posts