Node JS - Node js Bluebird Promises - Node - Node JS tutorial - webnode
What is Bluebird in Node.js?
- 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. This concept is used to ensure that every callback function which is called returns some sort of value.
What is Bluebird Promises in Node.JS?
- 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.
- 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.
Generating promises with the BlueBird library:
- 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.
Sample Code: -
Learn Node js - node js Tutorial - installation code in node.js bluebird promises - node - Node js Examples
- The require command is used to include the Bluebird library.
- Use Bluebird's .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.
- Step 3) The final step is to connect to our database, retrieve all the records in our collection and display them in our console log.
Learn Node js - node js Tutorial - database connection in node.js bluebird promises - node - Node js Examples
- You will notice that we are using the "connectAsync" method instead of the normal connection method for connecting to the database. Bluebird actually adds the Async keyword to each method in the MongoDB library to distinguish those calls which return promises and those which don't. So, there is no guarantee that methods without the Async word will return a value.
- Similar to the connectAsync method, we are now using the findAsync method to return all of the records in the mongoDB 'Employee' collection.
- Finally, if the findAsync returns a successful promise we then define a block of code to iterate through each record in the collection and display them in the console log.
If the above steps are carried out properly, all of the documents in the Employee collection will be displayed in the console as shown in the output below.
Learn Node js - node js Tutorial - employee collection in node.js bluebird promises - node - Node js Examples