์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- ํ๋ก๊ทธ๋๋ฐ์ด๋
- ์๋ฐ์คํฌ๋ฆฝํธ์ ํ์๋ฐฐ๊ฒฝ
- ๊ตฌ๋ฌธ๊ณผ ์๋ฏธ
- ๊ฐ์ฐ์ค์ ๊ณต์
- ์๋ฐ์คํฌ๋ฆฝํธ์ ECMAScript
- ES6 ๋ธ๋ผ์ฐ์ ์ง์ ํํฉ
- Math.min
- a && b
- for๋ฌธ
- ์๋ฆ๋๋ ์๊ณ ๋ฆฌ์ฆ
- for๋ฐ๋ณต๋ฌธ
- JavaScript
- ์์ค๋ฆ~
- .split()
- ์ด๋ ค์ด๋ฐใ ใ
- ํ๋ก๊ทธ๋๋จธ์ค ๊ณต์ ์ฐ์ฑ
- ํ๋ก๊ทธ๋๋จธ์ค
- async-await
- ์๋ฐ์คํฌ๋ฆฝํธ
- ์๊ณ ๋ฆฌ์ฆ
- array.reduce()
- arr.push()
- ํ๋ ฌ...
- ๋ฐฐ์ด ์ต์๊ฐ
- ์๋ฐ์คํฌ๋ฆฝํธ์ ํน์ง
- Ajax๋?
- .map()
- !x.includes()
- Promise.all()
- ์ผํญ์ฐ์ฐ์
Archives
- Today
- Total
Ming's develop story
Promise๋?(feat. async-await, Promise.all()) ๋ณธ๋ฌธ
Promise๋?
- ๋น๋๊ธฐ ์ฐ์ฐ์ ์ํ๋ฅผ ๋ํ๋ด๋ ๊ฐ์ฒด์ด๋ค.
- ๋น๋๊ธฐ ์ฒ๋ฆฌ๊ฐ ์งํ ์ค์ด๋ฉด pending, ์ฑ๊ณตํ๋ฉด fulfilled โ then() ๋ธ๋ก ์คํ, ์คํจํ๋ฉด rejected โ catch() ๋ธ๋ก ์คํ ์ํ๋ฅผ ๊ฐ์ง๋ค.
- Promise๋ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ then๊ณผ catch์ ์ฒด์ด๋์ ํตํด ๋ณด๋ค ๊ฐ๊ฒฐํ๊ฒ ํํํ ์ ์๋๋ก ES6์์ ๋์ ๋์๋ค.
โ Promise ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
const success = true;
if (success) resolve("๋ฐ์ดํฐ ๋ก๋ฉ ์๋ฃ!");
else reject("๋ฐ์ดํฐ ๋ก๋ฉ ์คํจ!");
}, 1000);
});
};
fetchData()
.then((result) => {
console.log(result); // "๋ฐ์ดํฐ ๋ก๋ฉ ์๋ฃ!"
})
.catch((error) => {
console.error(error); // "๋ฐ์ดํฐ ๋ก๋ฉ ์คํจ!"
});
Promise ๋ฑ์ฅ ์ด์ ์๋ ์ด๋ค ๋ฐฉ์์ผ๋ก ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ํ๋๊ฐ?
- ๊ณผ๊ฑฐ์๋ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ํ ๋ ์ฝ๋ฐฑ ํจ์ (callback function) ๋ฅผ ์ฌ์ฉํ๋ค.
- ์ฑ๊ณต ์ฝ๋ฐฑ๊ณผ ์คํจ ์ฝ๋ฐฑ์ ๊ฐ๊ฐ ๋๊ฒจ์ ์ฒ๋ฆฌํ๋ ๋ฐฉ์์ด ์ผ๋ฐ์ ์ด์๋ค.
- ํ์ง๋ง, ์ด๋ฐ ๋ฐฉ์์์ ๋ ๊ฐ ์ด์์ ๋น๋๊ธฐ ์์ ์ด ์์๋ฅผ ๊ฐ๊ณ ์คํ๋์ด์ผ ํ ๋, ์ฝ๋ฐฑ ํจ์ ์์ ๋ ๋ค๋ฅธ ์ฝ๋ฐฑ์ด ์ค์ฒฉ๋๋ callback hell ํ์์ด ๋ฐ์ํ๋ค.
โ ์ฝ๋ฐฑ ํฌ ์์
function fetchUser(callback) {
setTimeout(() => {
console.log("์ ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ");
callback();
}, 1000);
}
function fetchPosts(callback) {
setTimeout(() => {
console.log("์ ์ ์ ๊ฒ์๊ธ ๊ฐ์ ธ์ค๊ธฐ");
callback();
}, 1000);
}
function fetchComments(callback) {
setTimeout(() => {
console.log("๊ฒ์๊ธ์ ๋๊ธ ๊ฐ์ ธ์ค๊ธฐ");
callback();
}, 1000);
}
// ์ฝ๋ฐฑ ํฌ ๋ฐ์
fetchUser(() => {
fetchPosts(() => {
fetchComments(() => {
console.log("๋ชจ๋ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ ์๋ฃ!");
});
});
});
- ์ฝ๋ฐฑ ํจ์๊ฐ ์ค์ฒฉ๋ ์๋ก ์ฝ๋๊ฐ ๊น์ด์ง๋ฉฐ ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๋ค.
- ์ ์ง๋ณด์์ฑ์ด ๋ฎ์์ง๊ณ , ๋๋ฒ๊น ์ด ์ด๋ ค์์ง๋ค.
โ Promise๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ๋ ์ฑ์ด ๊ฐ์ ๋๋ค.
function fetchUser() {
return new Promise((resolve) => {
setTimeout(() => {
console.log("์ ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ");
resolve();
}, 1000);
});
}
function fetchPosts() {
return new Promise((resolve) => {
setTimeout(() => {
console.log("์ ์ ์ ๊ฒ์๊ธ ๊ฐ์ ธ์ค๊ธฐ");
resolve();
}, 1000);
});
}
function fetchComments() {
return new Promise((resolve) => {
setTimeout(() => {
console.log("๊ฒ์๊ธ์ ๋๊ธ ๊ฐ์ ธ์ค๊ธฐ");
resolve();
}, 1000);
});
}
fetchUser()
.then(fetchPosts)
.then(fetchComments)
.then(() => console.log("๋ชจ๋ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ ์๋ฃ!"));
async-await์ ๋ฌด์์ธ๊ฐ?
- Promise์ ์๋ฃ๋ฅผ ๊ธฐ๋ค๋ฆฌ๊ธฐ ์ํ ๋ ์ง๊ด์ ์ธ ๋ฌธ๋ฒ์ด๋ค.
- async ํค์๋๋ก ์ ์ํ ํจ์ ๋ด์์ ํธ์ถ๋๋ Promise ์์ await ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด, ํด๋น Promise๊ฐ ์๋ฃ๋ ๋๊น์ง ์คํ์ด ์ผ์ ์ค๋จ๋๋ค.
- ์ด๋ฅผ ํตํด ๋น๋๊ธฐ ์ฝ๋๋ฅผ ๋ง์น ๋๊ธฐ ์ฝ๋์ฒ๋ผ ์ฝ๊ฒ ์์ฑํ ์ ์๋ค.
โ async-await ์ฌ์ฉ ์์
async function fetchData() {
try {
console.log("์ ์ ์ ๋ณด ๊ฐ์ ธ์ค๋ ์ค...");
await fetchUser();
console.log("์ ์ ๊ฒ์๊ธ ๊ฐ์ ธ์ค๋ ์ค...");
await fetchPosts();
console.log("๊ฒ์๊ธ ๋๊ธ ๊ฐ์ ธ์ค๋ ์ค...");
await fetchComments();
console.log("๋ชจ๋ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ ์๋ฃ!");
} catch (error) {
console.error(error);
}
}
fetchData();
- await์ ์ฌ์ฉํ๋ฉด then() ์ฒด์ด๋ ์์ด ๋๊ธฐ์ ์ธ ์ฝ๋์ฒ๋ผ ๋ณด์ด๋๋ก ์์ฑํ ์ ์๋ค.
async-await ์ฌ์ฉ ์ ์ฃผ์ํ ์
โ ์๋ฌ ํธ๋ค๋ง
- try-catch ๋ธ๋ก์ ์ฌ์ฉํด ์๋ฌ๋ฅผ ๋ฐ๋์ ์ฒ๋ฆฌํด์ผ ํ๋ค.
- await์ด ์๋ ๋ถ๋ถ์์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด, ๋ค์ ์ฝ๋ ์คํ์ด ์ค๋จ๋๋ฏ๋ก ์๋ฌ๋ฅผ ์ก์์ฃผ์ง ์์ผ๋ฉด ์๊ธฐ์น ์์ ๋์์ด ๋ฐ์ํ ์ ์๋ค.
โ ์๋ฌ ํธ๋ค๋ง ์์
async function fetchData() {
try {
const result = await fetchDataFromAPI(); // ์คํจํ ๊ฐ๋ฅ์ฑ์ด ์์
console.log(result);
} catch (error) {
console.error("์๋ฌ ๋ฐ์:", error);
}
}
โ Promise.all()
- Promise.all()์ ์ฌ๋ฌ ๊ฐ์ Promise๋ฅผ ๋์์ ์คํํ ๋ ์ฌ์ฉ๋๋ค.
- ๋ชจ๋ Promise๊ฐ fulfilled ์ํ๊ฐ ๋์ด์ผ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ค.
- ํ๋๋ผ๋ rejected ์ํ๊ฐ ๋๋ฉด ์ ์ฒด๊ฐ ์คํจํ๋ค.
โ Promise.all() ์ฌ์ฉ ์์
function fetchUser() {
return new Promise((resolve) => {
setTimeout(() => resolve("์ ์ ์ ๋ณด"), 1000);
});
}
function fetchPosts() {
return new Promise((resolve) => {
setTimeout(() => resolve("๊ฒ์๊ธ ๋ฐ์ดํฐ"), 2000);
});
}
function fetchComments() {
return new Promise((resolve) => {
setTimeout(() => resolve("๋๊ธ ๋ฐ์ดํฐ"), 1500);
});
}
async function fetchAllData() {
try {
const [user, posts, comments] = await Promise.all([
fetchUser(),
fetchPosts(),
fetchComments(),
]);
console.log(user); // "์ ์ ์ ๋ณด"
console.log(posts); // "๊ฒ์๊ธ ๋ฐ์ดํฐ"
console.log(comments); // "๋๊ธ ๋ฐ์ดํฐ"
} catch (error) {
console.error("๋ฐ์ดํฐ ๋ก๋ฉ ์ค ์๋ฌ ๋ฐ์:", error);
}
}
fetchAllData();
โ Promise.all() vs await ๋น๊ต
๋ฐฉ์ ์คํ ๋ฐฉ์ ์์
Promise.all() | ๋ณ๋ ฌ ์คํ (๋์์ ์์) | await Promise.all([fetchUser(), fetchPosts()]) |
await ๊ฐ๋ณ ์คํ | ์์ฐจ ์คํ (ํ๋์ฉ ์๋ฃ ํ ๋ค์ ์คํ) | await fetchUser(); await fetchPosts(); |
๐น ์ธ์ Promise.all()์ ์ฌ์ฉํด์ผ ํ๋?
- ์ฌ๋ฌ ๊ฐ์ ๋น๋๊ธฐ ์์ ์ด ์๋ก ์์กดํ์ง ์๊ณ ๋์์ ์คํ๋ ์ ์์ ๋.
- ๋คํธ์ํฌ ์์ฒญ์ ์ต์ ํํ๊ณ ์ถ์ ๋.
๐ ์ ๋ฆฌ
- Promise๋ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ์ํ ๊ฐ์ฒด์ด๋ฉฐ, then()๊ณผ catch() ์ฒด์ด๋์ ํ์ฉํ๋ค.
- async-await์ ์ฌ์ฉํ๋ฉด ๋น๋๊ธฐ ์ฝ๋๋ฅผ ๋๊ธฐ ์ฝ๋์ฒ๋ผ ์ฝ๊ฒ ์์ฑํ ์ ์๋ค.
- try-catch๋ฅผ ํ์ฉํ์ฌ ์๋ฌ๋ฅผ ์์ ํ๊ฒ ์ฒ๋ฆฌํด์ผ ํ๋ค.
- Promise.all()์ ์ฌ์ฉํ๋ฉด ์ฌ๋ฌ ๊ฐ์ Promise๋ฅผ ๋ณ๋ ฌ๋ก ์คํํ ์ ์์ด ์ฑ๋ฅ ์ต์ ํ๊ฐ ๊ฐ๋ฅํ๋ค.
๋ค์ ๊ธ์ ๋๊ธฐ, ๋น๋๊ธฐ์ ๋ํด ๋ค๋ค๋ณด์.
'D E V E L O P ๐ป > CS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋๊ธฐ(Synchronous), ๋น๋๊ธฐ(Asynchronous) (0) | 2025.02.14 |
---|---|
DOM ์ ๊ฐ๋ (0) | 2025.02.10 |
Comments