Promises VS Callback in Nodejs

Posted By : Pankaj Kumar Yadav | 14-Oct-2016

Promises vs Callback in NodeJS

Hi All,

In this blog I'm going to show the difference between callback and Promises in Nodejs

First let's start with callbacks.

Due to non-blocking I/O, Node is heavy use of callbacks. All the APIs of Nodejs support callbacks.
A callback is a function called at the completion of a given task. This prevents any blocking, and allows other code to be run in the meantime. 

For example - Let there are four functions functionCall, doSomeworkOne, doSomeworkTwo, doSomeworkTwo and they are performing some IO tasks.
function doSomeworkThree functionCall depends doSomeworkOne, doSomeworkOne depends doSomeworkTwo, doSomeworkTwo depends doSomeworkThree. To make these sync, callback function passed as parameter in all functions.

 function functionCall(data, callback){
    ...........
    ...........
    doSomeworkOne(data, callback);
}
 
function doSomeworkOne(data, callback){
    ...........
    ...........
    doSomeworkTwo(otherData, callback);        
}
 function doSomeworkTwo(otherData, callback){
    ...........
    ...........
    doSomeworkThree(otherData, callback);
}
 function doSomeworkThree(otherData, callback){
    ...........
    ...........
    callback(result);
}
 function callback(data){
    return data
}
 

callback is good. The main problem with callbacks is: nested inside of callbacks, nested inside of callbacks. In nested callbacks, it is very tough to test/maintain the codes.
 Here the Promises comes. Promises provide us with a cleaner and more robust way of handling async code. Instead of using a callback. And also handling errors with promises is very easy.

See the example:

function functionCall(data){
    doSomeworkOne(data).then(function(data){
        return doSomeworkTwo(data);
    }).then(function(data){
        return     doSomeworkThree(data);
    }).catch(function(e) {
        // error handle    
    });
}
 function doSomeworkOne(data){
    retrun new Promise(function(resolve, reject){
        ...........
        ...........
        if(error){
            reject(error);        
        }else{
            resolve(success);
        }
    })        
}
 function doSomeworkTwo(data){
    retrun new Promise(function(resolve, reject){
        ...........
        ...........
        if(error){
            reject(error);        
        }else{
            resolve(success);
        }
    })        
}
function doSomeworkThree(data){
    retrun new Promise(function(resolve, reject){
        ...........
        ...........
        if(error){
            reject(error);        
        }else{
            resolve(success);
        }
    })        
}

 

Note: Promises and Callbacks are not fundamentally different. Promises is advisable in nested callbacks where you want to perform a series of actions.

I hope this will help you.

Thanks.

About Author

Author Image
Pankaj Kumar Yadav

Pankaj has been working as a Grails developer expertise in struts, spring, ejb, hibernate and angularjs framework.

Request for Proposal

Name is required

Comment is required

Sending message..