Node.js is great at times, but in some ways it’s a real pain in the ass.

Sure, it can create WebSocket servers, and it seems to be on the cutting edge of everything, but try to update an array of objects in a database then output a message and you’re in trouble.

This is because Node.js is an asynchronous language, meaning things don’t have to happen in the exact order they are written. The process of updating a database item in an array takes some time and Node.js knows this, so it starts the update and then just moves on trying to update the next item in the array. Once the update is complete a callback function is run, but by that point the loop has completed and there is no way to know which items finish in what order.

This is a real pain in the ass.

This is where the async library comes in handy. Async is a Node.js (NPM) and client side (Bower) JavaScript library that provides a better control flow mechanism which allows you to keep track of order and variables, like i, even when doing complex asynchronous database updates. Async provide two functions that I use very regularly, each() and eachSeries(). They both accept three parameters. The first is an array, the second is an iterator function, the third is a complete function.

var i = 0;

async.each(listItems, function(listItem, next) {

	listItem.position = i;

	listItem.save(function(err, results) {

		// i is increased because we need it on line 5
		i++;

		// the next() function is called when you
		// want to move to the next item in the array
		next();

	});

}, function(err) {

	// all data has been updated
	// do whatever you want

});

The iterator function is the key to how everything works. It’s pasted two parameters by async. The first is the individual object in the array you’re working with. Imagine the array forEach() loop. The second perimeter is a callback iterator function. You manually call this function whatever you want to move the loop to the next object in the array. This allows you to control when the loop iterates.

However, my favorite part of all this is the final callback. When your loop completes this function is run and is passed one parameter, an error object that is only populated if an error occurs. Inside this function you can output JSON, HTML, or emit a socket event back to the user.

Before I discovered async I was lost when it came to loops in Node.js. Now I feel like I have a good tool under my belt for controlling the flow of an application.

Demo Repo

Asynchronous Loops in Node.js Are Driving Me Loopy