Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Traditional JavaScript and Node.js API will utilize the concept of asynchronous callbacks for virtually every input/output operation. Whether it's reading a disk file, accessing a database table, or calling a web service, the results are not directly returned to the caller; instead they are passed into a separate function referred to as a callback. Because of JavaScript design, the only way to "freeze" a computation and have the "rest of it" execute latter (asynchronously) is to put "the rest of it" inside a callback.

For certain types of applications, this can be quite useful and performant; however, it also creates the problem more commonly known as Callback Hell. A lot of code ends up looking like this:

fs.readdir(source, function (err, files) {
  files.forEach(function (filename, fileIndex) {
    gm(source + filename).size(function (err, values) {
      aspect = (values.width / values.height);
      widths.forEach(function (width, widthIndex) {
        height = Math.round(width / aspect);
        this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) {
          if (err) console.log('Error writing file: ' + err)
        })
      })
    })
  })
})

Even when just a few simple API are being invoked, the pyramid shape and all the }) at the end are needed because each API call returns its data into an separate anonymous callback function.

This is generally not conducive to writing top-down transnational business applications, where everything happens in a procedural manner, one step at a time. Instead of:

fs.readdir(source, function (err, files) {
  // process files here
});

The business developer prefers this:

files = fs.readdir(source);
// process files here


  • No labels