๐Ÿ” [๋‹จ๊ณจ์งˆ๋ฌธ] - 04. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์— ๋Œ€ํ•œ ์„ค๋ช…

์กฐ์ค€ํ˜•ยท2021๋…„ 6์›” 17์ผ
0

interview

๋ชฉ๋ก ๋ณด๊ธฐ
5/20
post-thumbnail
  • ex) ์ฝœ๋ฐฑ, ํ”„๋กœ๋ฏธ์Šค, async await
  • ex) ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์˜ ํŠน์„ฑ ๋ฐ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•?

๐Ÿ’ฌ Answer

๐ŸŒ ๋™๊ธฐ์™€ ๋น„๋™๊ธฐ

๐Ÿ‘‰ ๋™๊ธฐ(synchronous)

๋™๊ธฐ๋ž€ ์š”์ฒญ์„ ๋ณด๋‚ธ ํ›„ ์‘๋‹ต์„ ๋ฐ›์•„์•ผ์ง€๋งŒ ๋‹ค์Œ ๋™์ž‘์ด ์ด๋ฃจ์–ด์ง€๋Š” ๋ฐฉ์‹
๋ชจ๋“  ์ผ์ด ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋จ.

๐Ÿ‘‰ ๋น„๋™๊ธฐ(Asynchronous)

์š”์ฒญ์„ ์ฆ‰์‹œ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์•„๋„ ๊ทธ ๋Œ€๊ธฐ ์‹œ๊ฐ„ ๋™์•ˆ ๋˜ ๋‹ค๋ฅธ ๋™์ž‘์ด ์ด๋ฃจ์–ด์ง€๋Š” ๋ฐฉ์‹
๋ชจ๋“  ์ผ์ด ๋ณ‘๋ ฌ์ ์œผ๋กœ ์‹คํ–‰


๐ŸŒ Callback๊ณผ Promise, Async & Await

๐Ÿ‘‰ Callback

  • ๋ง ๊ทธ๋Œ€๋กœ ๋‚˜์ค‘์— ํ˜ธ์ถœํ•  ํ•จ์ˆ˜.
  • ์ฒ˜๋ฆฌ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜๋ฅผ ์ค‘์ฒฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜์—ฌ Callback Hell์ด ๋ฐœ์ƒ
  • ์—๋Ÿฌ ํ•ธ๋“ค๋ง๋„ ๊ฐ ์ฝœ๋ฐฑํ•จ์ˆ˜๋งˆ๋‹ค ํ•ด์ค˜์•ผ๋˜์„œ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง.

๐Ÿ‘‰ Promise

  • ์–ด๋–ค ์ž‘์—…์˜ ์ค‘๊ฐ„ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” Object
  • ๋ฏธ๋ž˜์˜ ์–ด๋–ค ์ข…๋ฅ˜์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋จ์„ ์•ฝ์†ํ•ด์ฃผ๋Š” Object
  • ES6๋ถ€ํ„ฐ Callback์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•จ.
  • ์‹คํ–‰์ด ์ž˜ ์„ฑ๊ณตํ–ˆ๋Š”์ง€, ์‹คํŒจํ–ˆ๋Š”์ง€, ์„ฑ๊ณตor ์‹คํŒจ๊ฒฐ๊ณผ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๋ฐ˜ํ™˜
  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šคํ™”.
  • then๊ณผ catch๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๊ณ , finally๋Š” ์ฒ˜๋ฆฌ๋งŒ ๋˜๋Š” ํ•จ์ˆ˜

๐Ÿ‘‰ Async & Await

  • ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋‚˜์˜จ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ํŒจํ„ด
  • ๊ธฐ์กด์˜ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋ฐฉ์‹์ธ callback๊ณผ promise์˜ ๋‹จ์ ์„ ๋ณด์™„
  • ํ•จ์ˆ˜ ์•ž๋ถ€๋ถ„์— async, ํ•จ์ˆ˜ ๋‚ด๋ถ€์— Promise์˜ ์•ž๋ถ€๋ถ„์— await ์‚ฌ์šฉ.
  • ๋™๊ธฐ์ ์ธ ํ๋ฆ„์œผ๋กœ ๊ฐœ๋ฐœ์ด ๊ฐ€๋Šฅ.
  • ๊ฐœ๋ฐœ์ž๊ฐ€ ์ฝ๊ธฐ ์ข‹์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒํ•จ.
  • try catch๋กœ ํ•ธ๋“ค๋ง

๐Ÿ“˜ ์ฐธ๊ณ 

https://joshua1988.github.io/web-development/javascript/js-async-await/#async--await
https://sunnykim91.tistory.com/121

profile
๊นƒํ—ˆ๋ธŒ : github.com/JuneHyung

0๊ฐœ์˜ ๋Œ“๊ธ€