This is an example of a very common Node.js design pattern: a serial asynchronous iteration over a collection (array) using callbacks.
If you are interested in learning more about common (and even less common!) Node.js design patterns, you should check out the book Node.js Design Patterns by Mario Casciaro and Luciano Mammino (spoiler alert: that's me :P):
https://www.nodejsdesignpatterns.com/
To run the example:
- Download the two files (
test.jsanditerateSeries.js) into a folder - From your command line run
node test
In this realization if your collection doesn't contain anything (length = 0) program will create Zalgo problem. Because we expected that
iterateSeriesis an async function, but in case whencollectionlength = 0 it behaves like a sync function. So I would add this statementif (!collection.length) return process.nextTick(finalCallback)