Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what is a good pattern to wait for all iterations of async.eachLimit to finish before moving on? #1637

Closed
avieru opened this issue Apr 17, 2019 · 4 comments
Labels

Comments

@avieru
Copy link

avieru commented Apr 17, 2019

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?

@avieru
Copy link
Author

avieru commented Apr 17, 2019

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)

@avieru avieru closed this as completed Apr 17, 2019
@hargasinski
Copy link
Collaborator

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.

@avieru
Copy link
Author

avieru commented Apr 17, 2019

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.

@avalanche1
Copy link

Did you suceed? I'm facing the same - it simply doesnt wait for all iterations's promises to finish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants