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

v3.0 and eachSeries async/await #1647

Closed
endze1t opened this issue May 25, 2019 · 3 comments
Closed

v3.0 and eachSeries async/await #1647

endze1t opened this issue May 25, 2019 · 3 comments
Labels

Comments

@endze1t
Copy link

endze1t commented May 25, 2019

Hey,

v3.0 doesn't bring async/await support for eachSeries yet? Is there a full list, which async functions now support Promise?

Expected with this update:

async.eachSeries(items, async (item, callback) => {
            const itemStats = await someFunc(item);
           callback();
});

Currently in usage:

async.eachSeries(items, (item, callback) => {
         (async () => {
            const itemStats = await someFunc(item);
            callback();
          })();
});
@aearly
Copy link
Collaborator

aearly commented May 25, 2019

Async doesn't pass callbacks to async functions. Simply return!

async.eachSeries(items, async (item) => {
            const itemStats = await someFunc(item);
});

@endze1t
Copy link
Author

endze1t commented May 25, 2019

Sorry, my question wasn't that good.

Without callback() i'm not able to handle the error after:

async.eachSeries(items, async (item) => {
            const itemStats = await someFunc(item);
             
            if(!itemStats) throw new Error('no stats found');

}, (err) => {
       if(err) return res.status(500);

       return res.status(200).json({success: true})
});

This won't work without callback or?

@aearly
Copy link
Collaborator

aearly commented May 26, 2019

The idiomatic way to handle this with async/await would be:

try {
    await async.eachSeries(items, async (item) => {
            const itemStats = await someFunc(item);
             
            if(!itemStats) throw new Error('no stats found');
    })
} catch (err)  {
   return res.status(500);
}
return res.status(200).json({success: true})

@aearly aearly closed this as completed May 27, 2019
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

2 participants