
์ด๋ฒ์ ํ ์ค ์ฝํ ๋ฅผ ํ๋ฒ ๋ณด๊ฒ ๋์๋๋ฐ, ๋ฌธ์ ์ค์ Promise๋ฅผ ํธ๋ค๋ง ํด์ผํ๋ ๋ฌธ์ ๊ฐ ์์๋ค. ๋๋ ๋ถ๋ช ํ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ์ด์ ๋ ์๋ฒฝํ๊ฒ ํธ๋ค๋ง ํ ์ ์๊ฒ ๋ค๊ณ ์๊ฐํ๋๋ฐ, ๋ง์ Promise๊ฐ์ฒด๋ก ๋ ๋ฌธ์ ๋ฅผ ํ๋ค๋ณด๋ ์๊ฐ์ฒ๋ผ ๋์ง ์์๋ค..... (
์ด๊ฑธ ๋ชปํด,, ๋ต๋ตํ๋ค.)
๊ทธ๋์ ์ด์ฌ์ผ๋ก ๋์๊ฐ์ Promise๋ฅผ ๋ถ์ ๋ฒ๋ ค์ผ๊ฒ ๋ค๊ณ ์๊ฐํด์ ์ด๋ฒ ํฌ์คํ ์ ์์ฑํ๋ ๊ฒ์ด๋ค.
๐กJavaScript์์ Promise๋ ๋น๋๊ธฐ ์ฒ๋ฆฌ์ ํ์ค ๊ฐ์ฒด๋ค.
ํ์ง๋ง then๊ณผ catch ์ ๋๋ง ์ฐ๊ณ ๋์ด๊ฐ๊ฑฐ๋, async/await๋ก ๊ฐ์ธ์ "๋๊ธฐ์ฒ๋ผ" ๋ณด์ด๊ฒ๋ง ์ด๋ค.
๊ทธ๋ ๊ฒ ๋์ด๊ฐ๋ฉด ๋ฉด์ , ์ฝํ
, ์ค๋ฌด ๋๋ฒ๊น
์์ ๋ฐ๋์ ํ ๋ฒ์ ํฌ๊ฒ ๋นํ๋ค. (๋ง์น ๋์ฒ๋ผ,,,)
Promise๋ ๋ค์ 3๊ฐ์ง ์ํ(state)๋ฅผ ๊ฐ์ง๋ค.
| ์ํ | ์๋ฏธ | ์ ํ ๊ฐ๋ฅ์ฑ |
|---|---|---|
pending | ๋๊ธฐ ์ค | fulfilled ๋๋ rejected๋ก ์ ํ |
fulfilled | ์ดํ๋จ(์ฑ๊ณต) | ์ํ ๊ณ ์ |
rejected | ๊ฑฐ๋ถ๋จ(์คํจ) | ์ํ ๊ณ ์ |
const promise = new Promise((resolve, reject) => {
// ๋น๋๊ธฐ ์์
์ํ
if (/* ์ฑ๊ณต */) {
resolve("์ฑ๊ณต ๋ฐ์ดํฐ");
} else {
reject(new Error("์คํจ ์ด์ "));
}
});
๐จ ํ ๋ฒ ์ํ๊ฐ ๋ฐ๋๋ฉด ๋ค์ ๋ณ๊ฒฝ ๋ถ๊ฐ (Immutable)
console.log(1);
Promise.resolve().then(() => console.log(2));
console.log(3);
์ ์ฝ๋์ ์คํ ์์๋ 1 โ 3 โ 2 ์์ผ๋ก ์คํ๋๋๋ฐ ์ด๋ฅผ ์ดํดํ๋ ค๋ฉด Call Stack, Microtask Queue, Macrotask Queue ๋ฅผ ์์์ผ ํ๋ค.
Promise์ ๋์์ ์ดํดํ๊ธฐ ์ํ ์ธ๊ฐ์ง ๊ฐ๋
| ์ด๋ฆ | ์ญํ | ์์ |
|---|---|---|
| Call Stack (ํธ์ถ ์คํ) | ์ง๊ธ ์คํ ์ค์ธ ์ฝ๋์ ๋ชฉ๋ก | ๋๊ธฐ ์ฝ๋ ์คํ |
| Microtask Queue (๋ง์ดํฌ๋กํ์คํฌ ํ) | Promise์ .then ์ฝ๋ฐฑ ๊ฐ์, ์์ฃผ ๋น ๋ฅด๊ฒ ์คํํด์ผ ํ๋ ์์
๋๊ธฐ์ด | Promise.resolve().then(...) |
| Macrotask Queue (๋งคํฌ๋กํ์คํฌ ํ) | setTimeout, setInterval์ฒ๋ผ ๋ค์ ํด์ ์คํํ ์์
๋๊ธฐ์ด | setTimeout(..., 0) |
function a() { console.log("A"); }
function b() { a(); }
b();
// Call Stack: b() โ a() โ ์ฝ์ ์ถ๋ ฅ
.then/.catch/.finally ์ฝ๋ฐฑ์ด ์ฌ๊ธฐ์ ๋ค์ด๊ฐ๋ค.๐ก ๊ทธ๋์ ์์ ์ฝ๋์์ ์คํ์์๊ฐ 1 โ 3 โ 2๊ฐ ๋๋ ๊ฒ์ด๋ค.
setTimeout, setInterval๊ฐ์ ์์
์ด ๋ค์ด๊ฐ๋ค.console.log(1);
setTimeout(() => console.log(2), 0);
console.log(3);
// ์คํ ์์: 1 โ 3 โ 2
๊ฐ ์คํ๊ณผ ํ์ ๋ํด์๋ ์ถํ์ ๋ธ๋ก๊ทธ๋ก ๋ ์์ธํ๊ฒ ์์ฑ ์์
Promise๋ ๋ง์ดํฌ๋กํ์คํฌ ํ๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์, setTimeout๋ณด๋ค ํญ์ ๋จผ์ ์คํ๋๋ค๋ ๊ฒ์ ์์ผ๋ฉด ์๋๋ค.
setTimeout(() => console.log("timeout"), 0);
Promise.resolve().then(() => console.log("promise"));
// ์ถ๋ ฅ: promise โ timeout
๐ก then์ ์๋ก์ด Promise๋ฅผ ๋ฐํํ๋ค.
Promise.resolve(1)
.then(value => value + 1)
.then(value => value * 2)
.then(console.log); // 4
then์ ์๋ก์ด Promise๋ฅผ ๋ง๋ค์ด ๋ฐํthen์ ๋ฐํ๊ฐ์ด ๋ค์ then์ ์ฝ๋ฐฑ ์ธ์๋ก ์ ๋ฌ๋จreturn์ Promise๋ฅผ ๋ฃ์ผ๋ฉด ๊ทธ Promise๊ฐ resolve๋ ๊ฐ์ด ๋ค์ then์ผ๋ก ๋์ด๊ฐPromise.reject("์๋ฌ ๋ฐ์")
.then(res => console.log(res))
.catch(err => console.error("์๋ฌ:", err))
.finally(() => console.log("ํญ์ ์คํ"));
catch๋ ์ด์ ์ฒด์ธ์์ ๋ฐ์ํ ๋ชจ๋ ์๋ฌ๋ฅผ ์ก๋๋ค.finally๋ ์ฑ๊ณต/์คํจ์ ๋ฌด๊ดํ๊ฒ ์คํ๋๋ค.Promise.resolve()
.then(() => {
throw new Error("๋ฌธ์ ๋ฐ์!");
})
.catch(err => console.error(err.message)); // "๋ฌธ์ ๋ฐ์!"
then ๋ด๋ถ์์ ์์ธ ๋ฐ์ โ ์๋์ผ๋ก reject ์ํ๋ก ์ ํ
๐ก ๋ชจ๋ Promise๊ฐ ์ฑ๊ณตํด์ผ ์๋ฃ
๐ก ํ๋๋ผ๋ ์คํจํ๋ฉด ์ฆ์ reject
Promise.all([
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
]).then(console.log); // [1, 2, 3]
๐ก ์ฑ๊ณต/์คํจ ์๊ด์์ด ๋ชจ๋ Promise๊ฐ ๋๋ ๋๊น์ง ๊ธฐ๋ค๋ฆผ
Promise.allSettled([
Promise.resolve("OK"),
Promise.reject("Fail")
]).then(console.log);
๐ก ๊ฐ์ฅ ๋จผ์ settled๋ Promise์ ๊ฒฐ๊ณผ ๋ฐํ
Promise.race([
new Promise(res => setTimeout(() => res("A"), 100)),
new Promise(res => setTimeout(() => res("B"), 50)),
]).then(console.log); // "B"
๐ก ๊ฐ์ฅ ๋จผ์ ์ฑ๊ณตํ Promise ๋ฐํ
๐ก ์ ๋ถ ์คํจํ๋ฉด AggregateError ๋ฐ์
Microtask Queue, Macrotask Queue์ ์ฒ์ ์์๋ค์ ์ข์ ๊ธ ๊ฐ์ฌํฉ๋๋ค