Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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:

Code Block
languagejavascript
fs.readdir(source, function (err, files) {

 if (err) {
    console.log('Error finding files: ' + err)
  } else {
    files.forEach(function (filename, fileIndex) {

     console.log(filename)
      gm(source + filename).size(function (err, values) {
        if (err) {
          console.log('Error identifying file size:
' + err)
        } else {
          console.log(filename + ' : ' + values)
          aspect = (values.width / values.height)
 ;
        widths.forEach(function (width, widthIndex) {
   
        height = Math.round(width / aspect)
            console.log('resizing ' + filename + 'to ' + height + 'x' + height)
;
               this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) {
  
           if (err) console.log('Error writing file: ' + err)
            })
 
        }.bind(this))
  
     })
      })

   })
  }
}

The pyramid shape and all the }) at the end are needed because each API call returns its data into an anonymous callback function.