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

  • Things to Consider Before Hiring the Right Developer

    As the tech talent war continues to rage, it is no wonder that hiring the right developer takes forever. Managers face numerous difficulties not just hiring but retaining the right talent.If we are to go by statistics, the average time-to-hire for tech positions in the USA is 35 days. A realistic expectation is crucial while […]

  • 4 Ways To Reduce your App Development Costs by 50%

    Being multi-channel has become synonyms with the modern digital environment.Brand presence in these channels is critical to reach potential customers making it a vital business challenge of  the digital era.  Mobile apps have become an integral part of modern business. They are an extension of your brand. Considered as the most powerful business tool, apps […]

  • Tips to build a productive startup culture in your development company

    The word Startup paints a picture of tech-savvy professionals driving casual dress codes and sipping coffee from biodegradable cups in a space with enviable interior design, ping pong tables and bean bags. Ouch, the stereotype hurts! There is more to startups than bean bags and beer taps, it is the startup culture that fuels innovation […]

Leave a Reply

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