How about something like this?
# cat forEachAsync.js
function forEachAsync(array, fun, cb) {
var index = 0;
if (index == array.length) {
cb(null);
return;
}
var next = function () {
fun(array[index], function(err) {
if (err) {
cb(err);
return;
}
index++;
if (index < array.length) {
setImmediate(next);
return;
}
//We are done
cb(null);
});
};
next();
}
var columns = [1,2,3,4,5]
forEachAsync(columns, function(e, cb) {
console.log('changing column: ' + e);
// let them know we have done with this
// entry so we can start processin the
// next entry.
cb();
}, function(err) {
if (err) {
console.log('Failed in the process' + e.toString());
return;
}
console.log('## done ##');
});
# node forEachAsync.js
changing column: 1
changing column: 2
changing column: 3
changing column: 4
changing column: 5
## done ##
#