Jan 1st, 2020

How do I convert an existing callback API to promises?

If you are writing a lot of Node.js or just getting started and you will realize that all async code and most examples on npm make use of callbacks

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  console.log('body:', body); 
});

But consider a scenario where you make :

  1. API call #1
  2. Pass the response of API call #1 to API call #2
  3. Pass the response of API call #2 to API call #3

To accomplish that using the sample code above from npm for "request" you will end up with code like this:


request('http://www.example.com/api/call1', function(error, response, body) {
    request('http://www.example.com/api/call2?userId=body', function(error, response, body) {
        request('http://www.example.com/api/call3?productId=body', function(error, response, body) {
            console.log('body:', body);
        });
    });
});

Will it work? Yes. However, it consists of multiple nested callbacks which make the code to hard to read and debug. This is also referred to as callback hell. 

During the initial stages of the project, this kinda code may seem harmless however as the project grows and this pattern exists all over the place, you will end up in an undesirable situation.

A better way to handle this would be converting your callback methods to promises and use async-await e.g.

const request = require('request'); 
const makeRequest =(url) => {
    return new Promise(function(resolve, reject) {
        request(url, function(error, response, body) {
            if (error)
                reject(error)
            else
                resolve(body)
        });
    });
}

const resultCallOne = await makeRequest('http://www.example.com/api/call1')
const resultCallTwo = await makeRequest('http://www.example.com/api/call2?userId=' + resultCallOne.userId)
const resultCallThree = await makeRequest('http://www.example.com/api/call2?productId=' + resultCallTwo.productId)


Another example that will help you convert callbacks to promises:


function isIt(){
   let someOp = true
        
        if(someOp) 
           return 'its true'
        else 
           return 'no bueno'
}

//becomes

function isIt() {
    return new Promise(function(resolve, reject) {
        let someOp = true
        
        if(someOp) 
           resolve('its true')
        else 
           reject('no bueno')
    });
}

Was the article useful?

Your email address will not be published. Required fields are marked *