Check out async , it has an asynchronous foreach loop.
Edit
Here is the foreach method from the asynchronous library
async.forEach = function (arr, iterator, callback) { if (!arr.length) { return callback(); } var completed = 0; _forEach(arr, function (x) { iterator(x, function (err) { if (err) { callback(err); callback = function () {}; } else { completed += 1; if (completed === arr.length) { callback(); } } }); }); }; var _forEach = function (arr, iterator) { if (arr.forEach) { return arr.forEach(iterator); } for (var i = 0; i < arr.length; i += 1) { iterator(arr[i], i, arr); } };
source share