Refactoring Nested Callbacks in Node.js

May 22, 2015 David Sharer

As part of our efforts to share the latest technologies and capabilities in the world of digital marketing and web development, this post addresses Node.js – the highly impressive open source platform built on the Google Chrome JavaScript engine. To read more about Node.js, visit Introducing Node.js.

Though this post will primarily be relevant to developers, we hope the content remains interesting to all of our readers and provides some insight into the more technical aspects of the digital development world.

Background

One of the greatest wins for Node.js is the fact that all I/O is evented and executes as a part of the single threaded, asynchronous event loop. Of course, this is great for improved resource utilization; however, it also introduces an added complexity for the developer in the form of having to write nested callbacks.

Nested Callbacks

If you have ever needed to read/write from the file system or query a database multiple times, where the calls were interdependent, you most likely wrote nested callbacks. The more nested callbacks you have, the more error prone and unmaintainable your code will end up being. I, for one, can attest to having written very bad code because of this.

Finding a Solution

I knew there had to be a solution for this since there was much room for improvement. To escape from this callback nightmare, I recommend installing the async package from npm. As long as you are sure to implement your callback functions with arguments in the form that the documentation outlines, you can use the respective functions from the async library to abstract your nested callbacks. It is important to use a library like this to write maintainable code in Node. I believe nested callbacks violate the single responsibility software principle, as you now have the added responsibility of managing your callbacks.

Async Package

I have listed a few functions from the async package below. It’s worth checking out the documentation for yourself as there are many other great functions that this library provides. Async is also available for use within the browser. For more information go to www.npmjs.com/package/async.

From the async documentation:

waterfall(tasks, [callback])

  • tasks - An array of functions to run, each function is passed a callback(err, result1, result2, ...) it must call on completion. The first argument is an error (which can be null) and any further arguments will be passed as arguments in order to the next task.
  • callback(err, [results]) - An optional callback to run once all the functions have completed. This will be passed the results of the last task's callback.

series(tasks, [callback])

  • tasks - An array or object containing functions to run, each function is passed a callback(err, result) it must call on completion with an error err(which can be null) and an optional result value.
  • callback(err, results) - An optional callback to run once all the functions have completed. This function gets a results array (or object) containing all the result arguments passed to the task callbacks.

parallel(tasks, [callback])

  • tasks - An array or object containing functions to run. Each function is passed a callback(err, result) which it must call on completion with an error err (which can be null) and an optional result value.
  • callback(err, results) - An optional callback to run once all the functions have completed. This function gets a results array (or object) containing all the result arguments passed to the task callbacks.


Hopefully, you find this helpful while working within Node.js. This is a great technology and the performance is phenomenal. We look forward to implementing Node.js to help us build extraordinary solutions for our clients in the future.

Want to read more from One North Technologists? Check out our post on Sitecore vCards, introducing our API to make it as easy as possible to implement vCards on Sitecore sites.

Interested in learning more or joining the team? Let’s talk.

Subscribe

The One North Ideas Update delivers each month’s latest posts on digital for PSOs—including industry trends, news and our latest research—directly to your inbox. Although it’s our goal to always include thought-provoking and compelling content, you can unsubscribe at any time. 

See our Privacy Policy to learn more about how we protect and manage your submitted data.

David Sharer Senior Developer

At the time of publishing, David was a Senior Developer at One North.

One North Interactive 
222 North LaSalle St, #1500
Chicago, IL 60601

+1 312.469.1740