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

  • 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 […]

  • Meet React Native app 0.62 with Flipper

    Amid global pandemic,React Native app version 0.62 was released on March 26, 2020. It comes with the  support for Flipper by default. Introducing Flipper  Highly popular in the Android and iOS communities, Flipper is a developer tool for debugging mobile apps. In this release,  support by default has been enabled for new and existing React […]

  • Manage Remote Teams and Productivity During COVID-19

    With more than  471,862 cases around the globe, the coronavirus (COVID-19) pandemic has a devastating impact not only on the lives of hundreds of thousands of people but also on the global economy. The government aiming to contain and mitigate the disease has initiated the largest Coronavirus lockdown in the country. This has affected the […]

Leave a Reply

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