Node JS - Node js Bluebird Promises - Node - Node JS tutorial - webnode

What is Bluebird in Node.js?

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

var Promise = require('bluebird');

var mongoClient = Promise.promisifyAll(require('mongodb')).MongoClient;

var url = 'mongodb://localhost/EmployeeDB';

.then(function(db) {
        return db.collection('Employee').findAsync({})

    .then(function(cursor) {
        cursor.each(function(err, doc) {

Code Explanation:

 installation code in node.js bluebird promises

Learn Node js - node js Tutorial - installation code in node.js bluebird promises - node - Node js Examples

  1. The require command is used to include the Bluebird library.
  2. 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.

Code Explanation:-

 database connection in node.js bluebird promises

Learn Node js - node js Tutorial - database connection in node.js bluebird promises - node - Node js Examples

  1. 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.
  2. Similar to the connectAsync method, we are now using the findAsync method to return all of the records in the mongoDB 'Employee' collection.
  3. 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.

 employee collection in node.js bluebird promises

Learn Node js - node js Tutorial - employee collection in node.js bluebird promises - node - Node js Examples

This wikitechy technological portal provides you whole lot of information related to the topics such as mongodb tutorial , what is node js , express js tutorial , node js tutorial pdf , learn node js , node js tutorial for beginners , node js tutorial w3schools , node js express , javascript for beginners , node js express tutorial , node js tutorial point , node js book , node js server , note js , node js mongodb , what is node js used for , why node js , node tutorial , node js basics , tutorialspoint node js , node express , node js sample application , nodeschool , node js for beginners , express tutorial , node js application , use of node js , npm tutorial , node js org , node js training , node js online training , node js tutorial beginner , how to use node js , node js hello world , express node js , node js mongodb tutorial , nodejs http , why use node js

Related Searches to Node.js Bluebird Promises