๐Ÿ™…๐Ÿฝโ€โ™‚๏ธ FOREACH ๋‚ด๋ถ€์—์„œ AWAIT๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”.

6761 ๋‹จ์–ด beginnerswebdevjavascriptprogramming
์˜๋„ํ•œ ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋น ๋ฅธ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๊ณ ๋ คํ•˜๊ณ  ์ถœ๋ ฅ์„ ์ถ”์ธกํ•˜์‹ญ์‹œ์˜ค. ๋‚˜๋Š” ๋งค์ดˆ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ณ  resValue๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

const arr = [1 ,2 , 3 , 4]
let resValue = 1

const _1SecPromise = () => new Promise((res, rej) => { 
  setTimeout(() => { res(resValue++) }, 1000); 
})

const justFun  = async () => {
  console.log('๐Ÿš€ Start')

  arr.forEach(async (each) => {
    const val = await _1SecPromise()
    console.log("๐Ÿš€ each", val)

   })

  console.log('๐Ÿš€ End')

}

justFun()


๋‹น์‹ ์ด ๊ทธ๊ฒƒ์„ ๋งžํ˜”๋‹ค๋ฉด, ๋‹น์‹ ์€ ์ด๋ฏธ Generators๋ผ๋Š” ๊ฐœ๋…์„ ์•Œ๊ณ  ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 2๋ถ„ ์•ˆ์— ๋ฐฐ์šฐ์ง€ ์•Š์œผ๋ฉด.



๋ฐœ์ „๊ธฐ์˜ ๊ฐœ๋….


  • ๊ฐ„๋‹จํžˆ ๋งํ•ด์„œ ๊ฐ๊ฐ์€ ๋™๊ธฐ์‹์ธ ๋ณ„๋„์˜ ํ•จ์ˆ˜์—์„œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
  • ์ฆ‰, forEach๋Š” ๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ์˜ˆ์ƒํ•ฉ๋‹ˆ๋‹ค. ForEach๋Š” ์•ฝ์†์„ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ์ƒ์„ฑ๊ธฐ ์ž‘๋™ ๋ฐฉ์‹์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ์„ค๋ช…์€ ๋‹ค์Œ ์ด๋ฏธ์ง€
  • ์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


  • next() ๋ฉ”์„œ๋“œ๋Š” ๋™๊ธฐ์‹(์ตœ์†Œํ•œ forEach ๊ฒฝ์šฐ)๋งŒ ํ—ˆ์šฉํ•˜๋ฏ€๋กœ ๋Œ€๊ธฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์•ฝ์†์ด ์˜๋„ํ•œ ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

  • ๋Œ€๊ธฐ ๋ฐ ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์†”๋ฃจ์…˜์€ Good'ol for ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

    const justFun  = async () => {
      console.log('๐Ÿš€ Start')
      for ( let i = 0 ; i < arr.length ; i ++) {
        const val = await _1SecPromise()
        console.log('๐Ÿš€ each ', i )
    
      }
      console.log('๐Ÿš€ End')
    }
    


    ์ด๊ฒƒ์ด ์˜ˆ์ƒ๋Œ€๋กœ ์ž‘๋™ํ•  ๊ฒƒ์ด๋ผ๊ณ  ๋ฏฟ์œผ์„ธ์š”.



    ์•ฝ์†์˜ ๋ฐฐ์—ด์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋” ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด.
    ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ํ›Œ๋ฅญํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • Promise.all() ๋ฉ”์„œ๋“œ๋Š” ์•ฝ์†์˜ ์ดํ„ฐ๋Ÿฌ๋ธ”์„ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์ž…๋ ฅ ์•ฝ์†์˜ ๊ฒฐ๊ณผ ๋ฐฐ์—ด๋กœ ํ™•์ธ๋˜๋Š” ๋‹จ์ผ ์•ฝ์†์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • for...of statement์€ ๋‚ด์žฅ ๋ฌธ์ž์—ด, ๋ฐฐ์—ด, ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌํ•œ ๊ฐ์ฒด(์˜ˆ: ์ธ์ˆ˜ ๋˜๋Š” NodeList), TypedArray, Map, Set ๋ฐ ์‚ฌ์šฉ์ž ์ •์˜ iterable์„ ํฌํ•จํ•˜์—ฌ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฃจํ”„๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • ์˜๋„ํ•œ ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š” Map ๋ฐ Reduce๊ณผ ๊ฐ™์€ ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.



  • ์ด ๊ฒŒ์‹œ๋ฌผ์—์„œ ๋” ๋งŽ์€ ์„ค๋ช…์„ ์›ํ•˜๊ฑฐ๋‚˜ ์ผ๋ถ€ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์œผ๋ฉด ์˜๊ฒฌ์„ ๋‚จ๊ฒจ์ฃผ์„ธ์š”.

    ์ƒˆ๋กœ์šด ๊ฒƒ์„ ๋ฐฐ์› ๋‹ค๋ฉด ๐Ÿ’œ ๋ฅผ ์žŠ์ง€ ๋งˆ์„ธ์š”!

    ๐Ÿง‘๐Ÿฝโ€๐Ÿ’ป ์ผ๋ฐ˜ ๋ฐ ์ƒ์„ฑ๊ธฐ ๊ธฐ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๋Š” ์ž‘์€ ๊ทธ๋ฆผ์„ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉํ•œ ์•ฑ์„ ์•Œ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ๋ฐฉ๋ฌธ

    ๐Ÿ•ธ ๋‹น์‹ ์€ ์‹ ์ง„ ์›น ๊ฐœ๋ฐœ์ž์ด๋ฉฐ ๋ฐฉ๋ฌธ์—์„œ ๋ฐฐ์šธ ๋ฉ‹์ง„ CSS ์›น์‚ฌ์ดํŠธ๊ฐ€ ํ•„์š”ํ•˜์‹ญ๋‹ˆ๊นŒ?

    ๐Ÿช“ mongoDB ์‚ฌ์šฉ ? ์ฒดํฌ ์•„์›ƒ

    ์ง€์‹์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ด๋Ÿฌํ•œ ์œ ํ˜•์˜ ์ž‘์€ ๊ธฐ์‚ฌ๊ฐ€ ๋งˆ์Œ์— ๋“ค๋ฉด dev.to๋ฅผ ํŒ”๋กœ์šฐํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋งˆ์‹ญ์‹œ์˜ค. ๋” ๋งŽ์ด ์ž‘์„ฑํ•˜๊ณ  ์˜คํ”ˆ ์†Œ์Šค์— ๊ธฐ์—ฌํ•˜๋„๋ก ๋™๊ธฐ๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.

    ํ‰ํ™”๐Ÿ•Š !

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ