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

  • If you are in Jordan looking for app development support you should read this

    From downloading a mobile app  to visiting a brick-and-mortar store, customers today value every interaction with a brand. A great customer experience leaves a lasting impression in their minds about the happy and satisfying experience.  Winning the customer experience race Every  interaction offers  a make-or-break opportunity for a brand to either  build a loyal customer […]

  • Enhanced Efficiency & Optimized Cost of Project Development with Sweden Hybrid Model.

    Going beyond the cost factor and striving to deliver quality, innovation and flexibility to its customers across the globe, Toobler introduced a Hybrid (Onshore and Offshore) Software Development Model. Huge popularity of the hybrid model is fuelled by scores of organizations choosing to keep design and architecture phases at geographic proximity.  Driven by a customer-centric […]

  • 7 Pro Tips: Network Like a Boss at GITEX 2019

    The biggest tech show in the Middle East, North Africa & South Asia- GITEX Technology week takes place in Dubai World Trade Centre from Oct 6 – Oct 10. The opportunity to network with prospects, key decision-makers and business heads of the tech industry makes GITEX an unmissable event.  What makes the show exceptional is […]

Leave a Reply

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