Async: v3.0 ๋ฐ eachSeries ๋น„๋™๊ธฐ/๋Œ€๊ธฐ

์— ๋งŒ๋“  2019๋…„ 05์›” 25์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: caolan/async

์ด๋ด,

v3.0์€ ์•„์ง eachSeries์— ๋Œ€ํ•œ async/await ์ง€์›์„ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๊นŒ? ํ˜„์žฌ Promise๋ฅผ ์ง€์›ํ•˜๋Š” ๋น„๋™๊ธฐ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ „์ฒด ๋ชฉ๋ก์ด ์žˆ์Šต๋‹ˆ๊นŒ?

์ด ์—…๋ฐ์ดํŠธ์—์„œ ์˜ˆ์ƒ๋˜๋Š” ์‚ฌํ•ญ:

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

ํ˜„์žฌ ์‚ฌ์šฉ ์ค‘:

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

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

async / await ์ด๊ฒƒ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ด€์šฉ์  ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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})

๋ชจ๋“  3 ๋Œ“๊ธ€

๋น„๋™๊ธฐ๋Š” async ํ•จ์ˆ˜์— ์ฝœ๋ฐฑ์„ ์ „๋‹ฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ return !

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

์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค, ์ œ ์งˆ๋ฌธ์ด ์ข‹์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

callback()์ด ์—†์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

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})
});

์ฝœ๋ฐฑ ์—†์ด๋Š” ์ž‘๋™ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜?

async / await ์ด๊ฒƒ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ด€์šฉ์  ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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})

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰