๐Ÿ“’Javascript - async & await

zooyahoยท2022๋…„ 8์›” 4์ผ
0

async & await

  • await : ๋น„๋™๊ธฐ ํ•จ์ˆ˜(pomise๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜) ์•ž์— ์ž‘์„ฑ, asyncํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•จ!!
  • async : await์„ ๊ฐ€์ง€๋Š” ํ•จ์ˆ˜๊ฐ€ ์ž‘์„ฑ๋œ ํ•จ์ˆ˜ ์•ž์— ์ž‘์„ฑ

๐Ÿ‘พ#01 - ๋น„๊ตํ•˜๊ธฐ

๐Ÿ”ฅ promise๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์—๋Š” then()๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆœ์ฐจ์  ์ง„ํ–‰์„ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ.

๐Ÿ”ฅ ๋™๊ธฐ์ ์ธ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด async & awaitํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž„!

๐Ÿ‘พ #02 - fetch()๋ฅผ ์ด์šฉํ•œ ์˜ˆ์‹œ

const getData = async () => {
  console.log("start");
  const data = await fetch("https://jsonplaceholder.typicode.com/posts/1");
  const result = await data.json();
  console.log(result.title);
  console.log("end");
};
getData();

๐Ÿ˜ค ๋˜ํ•œ async๊ฐ€ ๋ถ™์€ ํ•จ์ˆ˜๋Š” ๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋˜๊ณ  ์ž๋™์œผ๋กœ promise๋ฅผ ๋ฐ˜ํ™˜ํ•จ!!!!!

์ถ”ํ›„ ๊ณต๋ถ€ํ•  ๋ฉ”์„œ๋“œ -> promise all | race
[์ƒํ™œ์ฝ”๋”ฉ]https://www.youtube.com/watch?v=a5AzftkvW9U

profile
์ฆ๊ฒ๊ฒŒ ๊ฐœ๋ฐœํ•˜์ž ์ฅฌ์•ผํ˜ธ๐Ÿ‘ป

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