Handling Synchronous Asynchronous Loops In Javascript/Node.JS
Historical note: I wrote this in 2014, before JavaScript had async/await.
Modern code can usually use a for loop with await for this, but I’m keeping
the original callback-based approach here for posterity.
One of the most lovable (yet frustrating) things about JavaScript and Node.js is the use of asynchronous functions and the callback-based system. Several times I've found myself wanting to halt for things to complete instead of sending off a bunch of processes all at once - an example being an array of URLs I want to ping and return the content of.
So, I'm going to outline how to create synchronous asynchronous loops in Node.js. By this, I mean a loop which runs asynchronous code a set number of times, waiting for each operation to complete before starting the next. The operations run sequentially without blocking the event loop. So without more ado, let's get to it!
Building the Loop
The first thing to note is that this is an extremely basic version, supporting looping through typical objects such as arrays, and incrementing by 1. It can, however, be adapted to almost any situation should you choose to. So, to start with, let's just define a simple function, along with our parameters.
function syncLoop(iterations, process, exit) {
// Body of the function
}
Just to talk about the params real quick;
iterations = the number of iterations to carry out
process = the code/function we're running for every iteration
exit = an optional callback to carry out once the loop has completed
So we have our function shell, now we need to instantiate an index, and a boolean to keep track of if we're done looping or not.
function syncLoop(iterations, process, exit) {
var index = 0,
done = false;
// Body of function
}
Now we can keep track of where we are, and if we're finished or not (both important in loops!). The
done boolean is going to be our way to check if we want to actually run again when called.
Right, this is where it gets slightly more complicated. We're going to create an object
loop which is actually our loop object, and we're going to return it so we can control the
loop from outside the function.
function syncLoop(iterations, process, exit) {
var index = 0,
done = false;
var loop = {
// Loop structure
};
return loop;
}
I'll come back to this in a little while. Ok, so we have our loop. What's important to have in a loop? Well, we need a way to access the index, move on in the loop, and a way to kill the loop - so let's implement these methods.
function syncLoop(iterations, process, exit) {
var index = 0,
done = false,
shouldExit = false;
var loop = {
next: function() {
// Don't start another iteration after a break
if (done && !shouldExit) {
return;
}
// Exit if we're done
if (done || index >= iterations) {
shouldExit = false;
done = true; // Make sure we say we're done
if (exit) {
exit(); // Call the callback on exit
}
}
// If we're not finished
index++; // Increment our index
process(loop); // Run our process, pass in the loop
},
iteration: function() {
return index - 1; // Return the loop number we're on
},
break: function(end) {
done = true; // End the loop
shouldExit = end; // Passing end as true means we still call the exit callback
}
};
return loop;
}
Ok, to talk through this a little bit;
-
The
loop.next()is our loop controller. Our process should callloop.next()when it wishes to complete an iteration and move to the next. Basically, allloop.next()does is call our desired process again, unless we're finished, in which case it calls the final callback. -
The
loop.iteration()function simply returns the index we're on. The first initialization means we'll always be one index ahead of the current iteration, so we returnindex - 1. -
The
loop.break()just tells the loop to finish on the current iteration. You can pass an optional value to tell the loop to end as normal and call theexit()callback if desired. This is useful for loops which need to clean up after themselves.
Right, we have a majority of the body here. So let's just kick it off, by calling
loop.next() just before we return our loop.
function syncLoop(iterations, process, exit) {
var index = 0,
done = false,
shouldExit = false;
var loop = {
next: function() {
// Don't start another iteration after a break
if (done && !shouldExit) {
return;
}
// Exit if we're done
if (done || index >= iterations) {
shouldExit = false;
done = true; // Make sure we say we're done
if (exit) {
exit(); // Call the callback on exit
}
return;
}
index++; // Increment our index
process(loop); // Run our process, pass in the loop
},
iteration: function() {
return index - 1; // Return the loop number we're on
},
break: function(end) {
done = true; // End the loop
shouldExit = end; // Passing end as true means we still call the exit callback
}
};
loop.next();
return loop;
}
Using the Loop
And we're done! All that matters now is implementing our loop and running it, so let's see an example;
syncLoop(5, function(loop) {
setTimeout(function() {
var i = loop.iteration();
console.log(i);
loop.next();
}, 5000);
}, function() {
console.log('done');
});
The above code simply prints out the current iteration with 5 seconds between each print, then logs done when complete. Go ahead and try it inside your browser console.
Stopping the Loop
Let's also check that our loop.break() works as expected.
var myLoop = syncLoop(5, function(loop) {
setTimeout(function() {
var i = loop.iteration();
console.log(i);
loop.next();
}, 5000);
}, function() {
console.log('done');
});
setTimeout(myLoop.break, 10000);
In this scenario, the break is requested after 10 seconds. An already scheduled callback will still run,
but its call to loop.next() won't start another iteration. Because we aren't passing a
boolean value to myLoop.break() it's not logging out done. We could change this
by using the following:
setTimeout(function() {
myLoop.break(true);
}, 10000);
One important thing to note is that you can't kill a loop (cleanly) whilst in mid execution, it will wait
until the current iteration is complete (which actually makes a lot of sense). The break is checked in
loop.next(), so it prevents another iteration from starting without cancelling work already
in progress.
Anyway, hopefully this was helpful - it's definitely been useful to me in my dealings with JavaScript and Node.js. If you have any questions/comments/suggested changes just let me know!