async await
ํ๋ก ํธ์๋๋ฅผ ๊ณต๋ถํ๋ฉด์ ์ ๋ง ๋ง์ด ์ ํ๋ ์์๊ฐ ๋ฐ๋ก async await ๊ตฌ๋ฌธ์ด๋ค. ํ์ง๋ง ๊ธฐ๊ณ์ ์ผ๋ก async์๋ await์ ์ฌ์ฉํด์ผ์ง ๋ผ๋ ์๊ฐ์ผ๋ก ๋ฌด๋ถ๋ณํ๊ฒ ์ฌ์ฉํ๊ณ ์๋๊ฒ ๊ฐ์ ์ด๋ฒ ๊ธฐํ์ ํด๋น ๊ตฌ๋ฌธ์ ์ข ์์ธํ ์ดํด๋ณด๊ณ ๊ณต๋ถํ๊ณ ์ ํ๋ค.
1๏ธโฃ async / await
๋จผ์ ๊ฐ๋จํ async/await์ ๋ํด ์์๋ณด์. async/await์ ์๋ฐ์คํฌ๋ฆฝํธ์ ๋น๋๊ธฐ ์ฒ๋ฆฌ ํจํด ์ค ํ๋์ด๋ค. ์ฌ์ฉํ๋ ค๋ ํจ์ ์์ async
๋ฅผ ๋ถ์ฌ์ ์ฌ์ฉํ๋ฉฐ, ํด๋น ๊ตฌ๋ฌธ์ ๋ถ์ธ ํจ์๋ ํญ์ Promise
๊ฐ์ฒด๋ฅผ ๋ฐํํ๊ณ , Promise๊ฐ ์๋ ๊ฐ์ ๋ฐํํ๊ฒ ๋๋ค๋ฉด Promise๋ก ๊ฐ์ ๊ฐ์ธ ๋ฐํ์ํจ๋ค.
await๋ async ํจ์ ์์์ ์ฌ์ฉ๋๋ค. Promise๊ฐ ์ฒ๋ฆฌ๋ ๋๊น์ง ํจ์ ์คํ์ ๊ธฐ๋ค๋ฆฌ๊ฒ ํ๋ค. ์ดํ Promise๊ฐ ์ฒ๋ฆฌ๋๋ฉด ์ฒ๋ฆฌ๊ฒฐ๊ณผ์ ํจ๊ป ์คํ๋๋ค.
async function Test() {
return "Finish";
}
const doTest = Test();
console.log(doTest);
function Test2() {
return new Promise((resolve, reject) => {
resolve("Finish");
});
}
const doTest2 = Test2();
console.log(doTest2);
> Promise { 'Finish' }
> Promise { 'Finish' }
์ ์ฝ๋๋ฅผ ๋ณด๋ฉด Test
๋ผ๋ ํจ์๋ async๋ฅผ ์์ ๋ถ์ฌ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ํ๋ ํจ์์ด๋ค. ๋ฐํ๊ฐ์ด Promise๊ฐ ์๋๊ธฐ ๋๋ฌธ์ Promise๋ก ๊ฐ์ผ Promise { 'Finish' }
๋ฅผ ๋ฆฌํดํ๋ค. ๊ทธ๋ฆฌ๊ณ Test2
๋ผ๋ ํจ์๋ ์์ ๋ฐํ๊ฐ์ด Promise์ด๊ธฐ ๋๋ฌธ์ ๋ ํจ์์ ๋ฐํ๊ฐ์ ๊ฐ๋ค.
2๏ธโฃ ์์ธ์ฒ๋ฆฌ
์์์ ๋งํ๋ฏ์ด async ํจ์๋ ํญ์ Promise๋ฅผ ๋ฐํํ๋ค. ๋ง์ฝ ์์
์ ์ฑ๊ณต์ ์ผ๋ก ์ฒ๋ฆฌํ๋ฉด Promise.resolve()
๋ฅผ, ์คํจํ๋ฉด Promise.reject()
๋ฅผ ๋ฐํํ๋ค. ์ด Promise.reject()๋ฅผ ์ด๋์ ๋ฐ์ํ๊ฒ ํ ๊ฒ์ด๋๊ฐ ๋ฐ๋ก ์์ธ์ฒ๋ฆฌ์ ์์ ์ด๋ค.
์์๋ฅผ ํ๋ฒ ์์ฑํด๋ณด์
1. try/catch
async function test() {
console.log("Do Test!");
throw new Error("New Error in function test");
}
function main() {
try {
console.log("try start");
test();
console.log("try end");
} catch (err) {
console.error("catch err in main");
console.log(err);
return console.log("main end in catch");
}
return console.log("main end");
}
main();
>
try start
Do Test!
try end
main end
Error: New Error in function test
์ ์ฝ๋์ ์คํ๊ฒฐ๊ณผ๋ฅผ ์ดํด๋ณด์. ๋น๋๊ธฐ ํจ์๋ก ์ ์ธํ test
๋ผ๋ ํจ์์์ ์๋ฌ๊ฐ ๋ฐ์ํ์ง๋ง, ํด๋น ์๋ฌ๊ฐ catch
์์ ๋ฐ์ํ์ง ์๊ณ , main
ํจ์๋ฅผ ๋ชจ๋ ์คํํ ์ดํ์ ๋ฐ์ํ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
์ด์ ๋
main
ํจ์๋ ๋น๋๊ธฐ ํจ์์ธtest
ํจ์๊ฐ Promise๋ฅผ ์ฒ๋ฆฌํ๋ ๊ณผ์ ์ ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ์คํ๋๊ธฐ ๋๋ฌธ์ด๋ค.
testํจ์์ ๋ฐํ๊ฐ์ด ์ฒ๋ฆฌ๋ ์์ ์ ์ด๋ฏธ mainํจ์์ try/catch๋ฌธ์ ๋ชจ๋ ์คํ์ ์๋ฃํ๊ธฐ ๋๋ฌธ์ catch๋ฌธ์ ๊ฑธ๋ฆฌ์ง ์์ ๊ฑฐ๋ถ๋ Promise๋ฅผ ์ฒ๋ฆฌ ํ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
๋ฐ๋ผ์, ๋น๋๊ธฐ ํจ์์ ์๋ฌ๋ฅผ catch๋ฌธ์์ ์ก๊ธฐ ์ํด์๋
await ์ฌ์ฉ
๋น๋๊ธฐ ํจ์ ํธ์ถ์์ catch๋ฌธ ์ฐ๊ฒฐ
์ด๋ผ๋ 2๊ฐ์ง ๋ฐฉ๋ฒ์ ์ฌ์ฉํ ์ ์๋ค.
2. await์ ์ด์ฉํ try/catch
๋จผ์ await์ ์ฌ์ฉํ์ฌ ์๋ฌ์ฒ๋ฆฌ๋ฅผ ํด๋ณด์
async function test2() {
console.log("test2 start");
throw new Error("New Error in function test");
}
async function main() {
try {
console.log("try start");
await test2();
console.log("try end");
} catch (err) {
console.error("catch from main");
console.error(err);
return console.log("main end in catch");
}
return console.log("main end");
}
main();
>
try start
test2 start
catch from main
Error: New Error in function test
main end in catch
await๋ฅผ ์ฌ์ฉํ์ง ์์์๋์ ์คํ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฅธ๊ฒ์ ๋ณผ ์ ์๋ค. ๊ฐ๋จํ๊ฒ ์คํ ์์๋ฅผ ์์๋ณด๋๋ก ํ์
main ํจ์์ console.log("try start")๋ฅผ ์คํ.
await์ ์ฌ์ฉํ์ฌ test2 ํจ์๋ฅผ ์คํํ๊ธฐ ๋๋ฌธ์ ํด๋น ํจ์๊ฐ Promise๊ฐ์ฒด๋ฅผ ๋ฐํํ ๋๊น์ง ์ดํ ์คํ์ ๋๊ธฐํ๋ค
test2์ ์คํ ๊ฒฐ๊ณผ๊ฐ์ธ Promise.reject()๊ฐ ๋ฐํ๋์ด ๊ณง๋ฐ๋ก catch๋ฌธ์ ์คํํ๋ค.
catch๋ฌธ์ ์๋ console.error("catch from main")์ console.error(err)๋ฅผ ์คํํ๋ค.
catch๋ฌธ ์์ return์ผ๋ก ์ธํ์ฌ console.log("main end in catch") ์คํ ํ ํ๋ก๊ทธ๋จ์ด ์ข ๋ฃ๋๋ค.
await๋ฅผ ์ฌ์ฉํ์ฌ test2 ํจ์๋ฅผ ํธ์ถํ๊ธฐ ๋๋ฌธ์ Promise๋ฅผ ๋ฐํํ ๋๊น์ง ๊ธฐ๋ค๋ ธ๋ค๊ฐ ์ดํ์ ์ฝ๋๋ค์ด ์คํ๋๋ค.
๋ค์์ผ๋ก ์ง์ catch๋ฌธ์ ์ฐ๊ฒฐํ๋ ๋ฐฉ๋ฒ์ด๋ค.
3. ์ง์ catch๋ฌธ ์ฐ๊ฒฐ
async function test3() {
console.log("test3 start");
throw new Error("New Error in function test3");
}
function main() {
console.log("main start");
test3().catch((err) => {
console.error("catch from main");
console.error(err);
return console.log("main end in catch");
});
return console.log("main end");
}
main();
>
main start
test3 start
main end
catch from main
Error: New Error in function test3
2๋ฒ๊ณผ ๋ฌ๋ผ์ง์ ์ try/catch
๋ฌธ์ด ์ฌ๋ผ์ง๊ณ ๋น๋๊ธฐ ํจ์๋ฅผ ํธ์ถํ๋ ๋ถ๋ถ์ ์ง์ catch๋ฅผ ์ฐ๊ฒฐํ์ฌ ํด๋น ํจ์๊ฐ Promise๋ฅผ ์ฒ๋ฆฌํ ์ดํ์ catch๋ฌธ์ด ์คํ๋๋๋ก ์์ฑํ ์ฝ๋์ด๋ค.
๋ ๋ฐฉ๋ฒ ๋ชจ๋ ๋น๋๊ธฐํจ์ ์๋ฌ์ฒ๋ฆฌ์ ์ ์ฉํ์ง๋ง try catch๋ฅผ ์ฌ์ฉํ๋๊ฒ์ด ์กฐ๊ธ๋ ๊ฐ๊ฒฐํ๊ณ ๊น๋ํด ๋ณด์ธ๋ค.
3๏ธโฃ ์ ๋ฆฌ
์์ธ์ฒ๋ฆฌ 1๋ฒ๊ณผ 2๋ฒ์์ ๋ํ๋ฌ๋ฏ์ด ๋ ๋ฐฉ๋ฒ์ ๊ฐ์ฅ ํฐ ์ฐจ์ด์ ์ ์ด๋ ๋ถ๋ถ์์ ํ๋ก๊ทธ๋จ์ด ์๋ฌ๋ฅผ ๋ฐ์์ํค๊ณ ์ข ๋ฃ๋๋๋ ๊ฒ์ด๋ค.
๋จ์ํ๊ฒ try/catch๋ฅผ ์ฌ์ฉํ์ฌ ์ฝ๋๋ฅผ ์์ฑํ๋ ๊ณผ๊ฑฐ๋ฅผ ์๊ฐํด๋ณด๋ฉด ๋ด๊ฐ ์ง๊ธ ๊ตฌํํ๋ ๊ธฐ๋ฅ์ ์ด๋์ ์์ธ์ฒ๋ฆฌ๋ฅผ ํ ๊ฒ์ธ์ง ๋ช ํํ ์๊ฐํด๋ณธ์ ์ด ์์๋ ๊ฒ ๊ฐ๋ค.
์ฌ์ฉ์ ์ ์ฅ์์ ์๊ฐํด๋ณด์์๋, ์์ฒญ ์ค์ํ ์๋ฌ๊ฐ ์๋์ด์ ์ฑ์ ์ฌ์ฉํ๋๋ฐ ์ง์ฅ์ด ์์ด์ผ ํ๋ค. ํ์ง๋ง ์ด๋ฌํ ๋ถ๋ถ์ ๊ณ ๋ คํ์ง ์๋๋ค๋ฉด ์์์น ๋ชปํ ๋ถ๋ถ์์ ํ๋ก๊ทธ๋จ์ด ์ข ๋ฃ๋๊ฑฐ๋ ํด๋น ๊ธฐ๋ฅ์ ์ฌ์ฉ์ ๋ชปํ๊ฒ ๋์ด ์ฌ์ฉ์ ๊ฒฝํ์ด ๋งค์ฐ ๋จ์ด์ง๋ ๊ฒฐ๊ณผ๋ฅผ ๋ณ์์ ์๊ธฐ์ ํญ์ ์ผ๋์ ๋๊ณ ๊ฐ๋ฐ์ ํ๋ ์ต๊ด์ด ํ์ํด ๋ณด์ธ๋ค.
Last updated