async in Node
JavaScript callbacks are not readable when nested in many layers. Async library is a great tool to overcome this.
I find a good tutorial with case examples written by Sebastian Seilund. Introduced control flows are:
- async.parallel([taskFunctions], finalCallback)
- async.series( [taskFunctions], finalCallback) *use when task functions depend on each other.
- async.forEach([collection], taskFunctionforEach, finalCallback)
- async.forEachLimit([collection], cocurrency, taskFunctionforEach, finalCallback) *use when only limited concurrencies are allowed for taskFunctionforEach)
- aysnc.forEachSeries([collection], taskFunctionforEach, finalCallback) *do the same to forEachLimit with the concurrency of 1.
- async.queue() and queue.drain() * use when handling steamed data from outside resources
And those flows (parallel, series, forEach) can be used in combination in a nested form.