Close

Reviewing “callback hell” before the first cross-platform release of yield in node 0.11

As we all await the first cross-platform release of yield in node 0.11, we thought this would be the best time to recall the issue of  “callback hell”  presented first by Derek Kastner. So lets have a look at this API call below.


#!/usr/bin/env node
var request = require(’request’); // npm install request
var apiurl = ’http://nodejs.org/api/index.json’;
var callbackfn = function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
};
request(apiurl, callbackfn);

when executed this produces the following.

callbackhell

Sometimes we may want to pass callbacks into callbacks. For example, suppose that we want to write the results of the above API call to disk. Below is the messy way to do this.


#!/usr/bin/env node
var request = require('request');
var apiurl = 'http://nodejs.org/api/index.json';
var fs = require('fs');
var outfile = 'index2.json';
request(apiurl, function(error, response, body){
if(!error && response.statusCode == 200){
fs.writeFile(outfile, body,function(err){
if(err) throw err;
var timestamp = new Date();
console.log("wrote %s %s", outfile, timestamp);
});
}
});

This works, but it is bad in the following respects.

Firstly, its difficult to read

Secondly, it mixes three different functions together namely -> making the HTTP request, extracting the body from the HTTP response, and writing the body to disk.

Thirdly, its hard to debug and test in isolation (REPL inconvenience).

Fourthly, because the default output of request that is passed to the callback is the 3-tuple of (error, response, body),we’re doing

This form of asynchrony will make programs harder to reason about and it will lead to ugly nested callback issue.

One way around this is to use one of the node flow control libraries, like Caolan’s async. Another is to await the introduction of yield. 🙂

Latest stories

  • How Kerala’s IT Sector Braces To Fight COVID-19?

    In India and around the world, the impact of the coronavirus pandemic is evident across all sectors of the economy.  With  COVID-19 spreading rapidly around the world, the pandemic has not only affected the lives of hundreds of thousands of people but also demonstrated a growing impact on the global economy. From companies chalking out […]

  • How a DevOps Approach Helps To Meet Deadlines Every Time ?

    Missing a deadline never feels good!  Despite designing perfect workflows from start to finish, we often found our teams chasing deadlines until the very last moment. This upsetting trend led us to a quest to find out what obstacles prevented our teams from getting work over the finish line in time. The biggest factors contributing […]

  • Can You Fuel The Next Wave of Business Growth During a Pandemic?

    Pandemics and economic recessions are known to create disruptive effects on global economies and businesses. For e.g, the Black Death was a devastating bubonic plague that arrived in Europe during 1347. The catastrophe resulted in the collapse of the feudal system in Europe while replacing it with a more modern employment contract.  Covid-19 pandemic has […]

Leave a Reply

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