I have the following code using eachSeries:
var allLetters = ["1", "2", "3", "4"];
async.eachSeries(allLetters, function(arrObj, callback){
saveAllToS3(arrMain, arrObj);
console.log("Reach here");
}, function (err){
console.log("callback is called");
if (err){
console.log(err);
} else {
console.log("Completed");
}
});
I see that saveAllToS3 is execute only one time, and the callback is not called ("callback is called" is not printed, and also no error or "Completed" is printed).
Only "Reach here" is printed.
I want that saveAllToS3 will call 4 times. First, the function will be called with "0", after it finished, the function will be called with "1" and so on..
The point of async.*() is that the intermediate callbacks you pass are asynchronous – they aren't considered to be finished until they call their callback parameter.
Since you never call the callback, your first callback never finishes.
Async can only proceed to the next input item when you tell it that the current iteration is finished. You do this by invoking the callback that it provides to your function:
var allLetters = ["1", "2", "3", "4"];
async.eachSeries(allLetters,
function(arrObj, done){
saveAllToS3(arrMain, arrObj);
console.log("Reach here");
done();
},
function (err){
console.log("callback is called");
if (err) {
console.log(err);
} else {
console.log("Completed");
}
});
I find it useful to call the callback argument something like done to make the code more explicit.