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๋ฅผ ๋ฐํํจ!!!!!
- ์ฐธ๊ณ
[์ํ์ฝ๋ฉ]https://www.youtube.com/watch?v=1z5bU-CTVsQ&t=684s
์ถํ ๊ณต๋ถํ ๋ฉ์๋ -> promise all | race
[์ํ์ฝ๋ฉ]https://www.youtube.com/watch?v=a5AzftkvW9U