This is a regular code template that I often use when I don't need additional dependencies:
var tags = ['tag1', 'tag2', 'tag3']; var wait = tags.length; tags.forEach(function (tag) { doAsyncJob(tag, done); }); function done() { if (--wait === 0) allDone(); }
This code will run doAsyncJob (tag, callback) in parallel for each element of the array and call allDone when each job is completed. If you need to process data continuously (one after another), here is another template:
(function oneIteration() { var item = tags.shift(); if (item) { doAsyncJob(item, oneIteration); } else { allDone(); } })();
source share