Seize your moment! ๐Ÿ‘พ

์•ˆ๋…•ํ•˜์„ธ์š”. Eric์ž…๋‹ˆ๋‹ค. ์ œ ๋ธ”๋กœ๊ทธ์— ๋ฐฉ๋ฌธํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ป ๊ฐœ๋ฐœ๊ณต๋ถ€/JavaScript

[Eric's JS] JavaScript์—์„œ async/await๊ณผ forEach๋ฌธ ์‚ฌ์šฉ ๋ฌธ์ œ ๋ฐ ํ•ด๊ฒฐ์ฑ… ๐Ÿค”

Eric_ko 2024. 7. 29. 23:24

์–ผ๋งˆ์ „์— JavaScript๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋‹ค๊ฐ€,
JavaScript์—์„œ forEach ๋ฉ”์„œ๋“œ์™€ async/await๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ, ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๋™์ž‘์œผ๋กœ ์ฝ”๋“œ๊ฐ€
์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ œ๋ฅผ ๊ฒช์—ˆ๋‹ค.
์ฐพ์•„๋ณด๋‹ˆ ์ด๋Š” forEach ๋ฉ”์„œ๋“œ๊ฐ€ ๋น„๋™๊ธฐ ์ฝ”๋“œ๋ฅผ ๋™๊ธฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ ํ•œ๋‹ค.

๋ฌธ์ œ ์„ค๋ช…

forEach ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์ง€๋งŒ, ํ•ด๋‹น ์ฝœ๋ฐฑ์ด ๋น„๋™๊ธฐ ํ•จ์ˆ˜์ผ ๊ฒฝ์šฐ ์ด๋ฅผ ์ œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ•œ๋‹ค.
์ฆ‰, async/await๋ฅผ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ forEach๋Š” ๊ฐ ์ฝœ๋ฐฑ์˜ ์™„๋ฃŒ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ๋‹ค์Œ ์š”์†Œ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
์ด๋Š” forEach๊ฐ€ ๋‚ด๋ถ€์ ์œผ๋กœ ๋น„๋™๊ธฐ ์ฝ”๋“œ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ์‹ ๋•Œ๋ฌธ์ด๋‹ค.

์˜ˆ์ œ ์ฝ”๋“œ

๋‹ค์Œ์€ forEach์™€ async/await๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์ œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค:

const asyncOperation = async (num) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log(`Processed ${num}`);
      resolve(num);
    }, 1000);
  });
};

const processArray = async (array) => {
  array.forEach(async (num) => {
    await asyncOperation(num);
  });
  console.log('Done processing array');
};

processArray([1, 2, 3, 4]);

์ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์˜ˆ์ƒํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
'Done processing array'๊ฐ€ ๋จผ์ € ์ถœ๋ ฅ๋˜๊ณ  ๋‚˜์„œ ๊ฐ ์š”์†Œ๊ฐ€ ์ฒ˜๋ฆฌ๋œ๋‹ค.
์ด๋Š” forEach๊ฐ€ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ๋™๊ธฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ถœ๋ ฅ ๊ฒฐ๊ณผ

Done processing array
Processed 1
Processed 2
Processed 3
Processed 4

๋น„๋™๊ธฐ ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ 

forEach๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋™๊ธฐ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ๋‹ค๊ณ ํ•œ๋‹ค.
forEach์— ์ „๋‹ฌ๋œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ์ฆ‰์‹œ ์‹คํ–‰๋˜๋ฉฐ, forEach๋Š” ๊ฐ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์™„๋ฃŒ ์—ฌ๋ถ€๋ฅผ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๋Š”๋‹ค.
๋”ฐ๋ผ์„œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด์—์„œ async/await๋ฅผ ์‚ฌ์šฉํ•ด๋„, forEach๋Š” ๋น„๋™๊ธฐ ํ•จ์ˆ˜์˜ ์™„๋ฃŒ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ๋‹ค์Œ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ์„ ์‹คํ–‰ํ•œ๋‹ค.

forEach์˜ ๋™์ž‘ ๋ฐฉ์‹์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, forEach์˜ ๋‚ด๋ถ€ ๊ตฌ์กฐ๋ฅผ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ด ๋„์›€์ด ๋œ๋‹ค.
forEach๋Š” ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ๋ชจ๋“  ์ฝœ๋ฐฑ์ด ๋น„๋™๊ธฐ์ ์ด๋ผ๋ฉด, forEach๋Š” ์ด๋ฅผ ๋™๊ธฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๋‹ค.
์ด๋Š” forEach๊ฐ€ Promise๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

ํ•ด๊ฒฐ์ฑ…

forEach ๋Œ€์‹  for...of ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด async/await๋ฅผ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
for...of๋Š” ๊ฐ ๋น„๋™๊ธฐ ์ž‘์—…์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ ํ›„ ๋‹ค์Œ ์ž‘์—…์„ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋น„๋™๊ธฐ ์ฝ”๋“œ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฐœ์„ ๋œ ์ฝ”๋“œ

๋‹ค์Œ์€ for...of ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ ์˜ˆ์ œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค:

const asyncOperation = async (num) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log(`Processed ${num}`);
      resolve(num);
    }, 1000);
  });
};

const processArray = async (array) => {
  for (const num of array) {
    await asyncOperation(num);
  }
  console.log('Done processing array');
};

processArray([1, 2, 3, 4]);

์ถœ๋ ฅ ๊ฒฐ๊ณผ

Processed 1
Processed 2
Processed 3
Processed 4
Done processing array

์ด์ œ ๊ฐ ๋น„๋™๊ธฐ ์ž‘์—…์ด ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๊ณ , 'Done processing array'๋Š” ๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋œ ํ›„์— ์ถœ๋ ฅ์ด ๋˜๋Š”๊ฑธ ๋ณผ์ˆ˜์žˆ๋‹ค.

๊ฒฐ๋ก 

JavaScript์—์„œ forEach์™€ async/await๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๊ฐ€ ๋™๊ธฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š๋Š” ๋ฌธ์ œ๋ฅผ ๊ฒช์„ ์ˆ˜ ์žˆ๋‹ค.
์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด for...of ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
for...of๋Š” ๊ฐ ๋น„๋™๊ธฐ ์ž‘์—…์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋ฏ€๋กœ, ์›ํ•˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๋” ์ง๊ด€์ ์ด๊ณ  ์˜ค๋ฅ˜๊ฐ€ ์ ์€ ๋น„๋™๊ธฐ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.