question: bit new to async and have following code to download lots of files, 2 at about the same time
async.eachLimit(files, 2, async (file, callback) => {
reportStep("downloading");
console.log("async start: " + file.path);
await addFile(file.path, file.size);
console.log("async end: " + file.path);
callback();
})
reportStep("completed")
Of course, "completed" quickly get's fired while async.eachLimit continues executing funcs.
What is a good way to wait for all the iterations to be done?
nvm, didn't read the docs close enough.
there is yet another callback param, ended up wrapping it up all up in a promise that i can await
function download(files) {
return new Promise((resolve, reject) => {
async.eachLimit(files, 2, async (file, callback) => {
reportStep("downloading");
console.log("async start: " + file.path);
await addFile(file.path, file.size);
console.log("async end: " + file.path);
callback();
}, (error) => {
if (error){
reject(error);
} else {
resolve();
}
})
});
}
await download(files)
Side note, if you are using the prerelease of v3
, you don't need the wrapper. eachLimit
will return a promise if you don't pass the final callback.
hmm, I tried awaiting async.eachLimit but it wasn't working for some reason or another. Thats what prompted me to post here. I'll have to give it another shot.
Did you suceed? I'm facing the same - it simply doesnt wait for all iterations's promises to finish.