๐ 06. async/await โ Promise๋ฅผ ๋๊ธฐ ์ฝ๋์ฒ๋ผ ์ฝ๊ฒ ๋ง๋๋ ๋ง๋ฒ์ ์ค์ฒด
๐ ๊ฐ์
async/await์ ๋ด๋ถ ๋์ ์๋ฆฌ, ์๋ฌ ์ฒ๋ฆฌ ํจํด, ๋ณ๋ ฌ ์ต์ ํ, ์ค๋ฌด์์ ์์ฃผ ๋น ์ง๋ ํจ์ ๋ค์ ์์ ์ ๋ณตํฉ๋๋ค.
๐ฏ ์ด ์น์ ์ ์ฝ๊ณ ๋๋ฉด:
async/await์ด Promise์ ๋ฌธ๋ฒ์ ์คํ์์ ์ดํดํ๊ณ , ๋ด๋ถ ๋์์ ์ค๋ช ํ ์ ์๋ค.try/catch๋ก ๋น๋๊ธฐ ์๋ฌ๋ฅผ ๋น ์ง์์ด ์ฒ๋ฆฌํ๋ค.await์ ๋จ๋ฐํ์ ๋ ๋ฐ์ํ๋ ์ฑ๋ฅ ํจ์ ์ ์๋ณํ๊ณ ์ต์ ํํ๋ค.
๐ ๋ชฉ์ฐจ
- ๐ ์ด ๋ฌธ์๋ฅผ ์ฝ๊ธฐ ์ ์
- ๐ค 1. ์ ์์์ผ ํ๋๊ฐ?
- โ๏ธ 2. async/await์ ๋ณธ์ง
- ๐ก๏ธ 3. ์๋ฌ ์ฒ๋ฆฌ ํจํด
- โก 4. ์ฑ๋ฅ ์ต์ ํ โ await ๋จ๋ฐ ๊ธ์ง
- ๐ 5. ์ค๋ฌด ํจํด ๋ชจ์
- ๐ ๋ง๋ฌด๋ฆฌ ํด์ฆ
- ๐ฃ ์์ฒ ์ด์ ํด๊ทผ ์ผ๊ธฐ
- ๐ ๋ ์์๋ณด๊ธฐ
๐ ์ด ๋ฌธ์๋ฅผ ์ฝ๊ธฐ ์ ์
โฑ๏ธ ์์ ์ฝ๊ธฐ ์๊ฐ: 20๋ถ(์ ์ฒด) / ํต์ฌ ํํธ๋ง: 12๋ถ
๐บ๏ธ ์ด ๋ฌธ์์ ํ๋ฆ
[async/await = Promise] โ [์๋ฌ ์ฒ๋ฆฌ ์ ๋ต] โ [await ๋จ๋ฐ ์ฑ๋ฅ ํจ์ ] โ [์ค๋ฌด ํจํด]
๐ฏ ์ด ๋ฌธ์๋ฅผ ๋ค ์ฝ์ผ๋ฉด ํ ์ ์๋ ๊ฒ
-
async/await์ Promise ์ฒด์ด๋์ผ๋ก, Promise ์ฒด์ด๋์async/await์ผ๋ก ๋ณํํ ์ ์๋ค. -
await์ ์์ฐจ์ ์ผ๋ก๋ง ์ฐ๋ ์ฝ๋๋ฅผ ๋ณ๋ ฌ ์ต์ ํํ ์ ์๋ค. - ๋น๋๊ธฐ ์๋ฌ๋ฅผ
try/catch์ ๋ํผ ํจํด์ผ๋ก ๊ฒฌ๊ณ ํ๊ฒ ์ฒ๋ฆฌํ๋ค.
๐บ๏ธ ์ด ๋ฌธ์์ ๋ฐฐ๊ฒฝ ์ธ๊ณ๊ด: '์์๋ค ์ปค๋ฎค๋ํฐ'
๐ฃ ์์ฒ : "์ํธ ๋,
async/await์ฐ๋๊น ์ฝ๋๊ฐ ํจ์ฌ ์ฝ๊ธฐ ํธํด์ก์ด์! ๊ทผ๋ฐ ์์ ๋์ด ํ์ด์ง ๋ก๋ฉ์ด ๋๋ฆฌ๋ค๊ณ ํ์ ์ API ํธ์ถ ํ์ด๋ฐ์ ๋ณด๋๊น... ๊ฒ์๊ธ, ์์ฑ์, ๋๊ธ์ ํ๋์ฉ ๊ธฐ๋ค๋ฆฌ๊ณ ์๋๋ผ๊ณ ์. ์์๋๋กawaitํ๋ฉด ์ ๋๋ ๊ฑด๊ฐ์? ๋ญ๊ฐ ์ง๋ ฌ๋ก ์คํ๋๋ ๊ฒ ๊ฐ์์์."
๐ฆ ์ํธ: "์์ฒ ๋, ๋น๋๊ธฐ ์ฝ๋๊ฐ ํจ์ฌ ๊น๋ํด์ก๋ค์! ๊ทธ๋ฐ๋ฐ
await์ ์ฌ๋ฌ ๊ฐ ์์๋๋ก ์ฐ๋ฉด ์ฝ๋๊ฐ ์์ ํ ์ง๋ ฌ๋ก ์คํ๋๋ค๋ ์ ์ ์ฃผ์ํด์ผ ํด์. 1์ด์ง๋ฆฌ ์์ฒญ 3๊ฐ๋ฅผ ๊ฐ๊ฐawaitํ๋ฉด 3์ด๊ฐ ๊ฑธ๋ฆฌ๊ฑฐ๋ ์. ๋ง์ฝ ์ธ API๊ฐ ์๋ก๋ฅผ ๊ธฐ๋ค๋ฆด ํ์๊ฐ ์๋ค๋ฉดPromise.all๋ก ๋์์ ๋ ๋ ค๋ณด์ธ์. 1์ด ๋ง์ ๋๋ผ ์ ์์ ๊ฑฐ์์."
๐ค 1. ์ ์์์ผ ํ๋๊ฐ?
์ํธ ๋ฆฌ๋ ๋์ด ์์ฒ ์ด PR์ ๋จ๊ณจ๋ก ๋จ๊ธฐ๋ ๋ฆฌ๋ทฐ ์ฝ๋ฉํธ๊ฐ ์๋ค: "async/await ์ 'ํธํ ๋ฌธ๋ฒ'์ผ๋ก๋ง ์ฐ๋ฉด ๋ฐ๋์ ํจ์ ์ ๋น ์ง๋๋ค." async/await์ ํ๋ JS ๋น๋๊ธฐ ์ฝ๋์ ํ์ค ๋ฌธ๋ฒ ์ด๋ค. ํ์ง๋ง ๋จ์ํ "ํธํ ๋ฌธ๋ฒ"์ผ๋ก๋ง ์๊ณ ์ฐ๋ฉด ๋ฐ๋์ ํจ์ ์ ๋น ์ง๋ค. ๋ด๋ถ๊ฐ Promise๋ผ๋ ๊ฑธ ์์์ผ:
- ์๋ฌ ์ฒ๋ฆฌ๋ฅผ ๋น ๋จ๋ฆฌ์ง ์๋๋ค.
- ๋ถํ์ํ ์ง๋ ฌ ์คํ์ผ๋ก ์ฑ๋ฅ์ ๋ญ๋นํ์ง ์๋๋ค.
- ๋ฐํ๊ฐ์ด Promise์์ ์ดํดํ๊ณ ์ฌ๋ฐ๋ฅด๊ฒ ์กฐํฉํ๋ค.
โ๏ธ 2. async/await์ ๋ณธ์ง
async ํจ์๋ ํญ์ Promise๋ฅผ ๋ฐํํ๋ค
async๊ฐ ๋ถ์ ํจ์๋ ์ด๋ฏธ ์ฝ์(Promise)์ด ๋ดํฌ๋์ด ์์ต๋๋ค. ๋ด๋ถ์์ ํ๋ฒํ ๋ฌธ์์ด์ด๋ ์ซ์๋ฅผ ๋ฐํํด๋, ์์ง์ด ์ ์ฑ์ค๋ฝ๊ฒ Promise๋ก ๊ฐ์ธ์ ๋ด๋ณด๋
๋๋ค.
// async ํจ์๊ฐ ๋ฐํํ๋ ๊ฒ
async function getPostTitle() {
return "ํด๋ก์ ์์ ์ ๋ณต"; // ๋จ์ ๊ฐ์ ๋ฐํํด๋...
}
const result = getPostTitle();
console.log(result); // Promise { 'ํด๋ก์ ์์ ์ ๋ณต' } โ Promise๋ก ๋ํ๋จ!
// .then์ผ๋ก ๊ฐ ๊บผ๋ด๊ธฐ
result.then((title) => console.log(title)); // "ํด๋ก์ ์์ ์ ๋ณต"
// ๋๋ ๋ ๋ค๋ฅธ async ํจ์์์ await์ผ๋ก
async function main() {
const title = await getPostTitle(); // "ํด๋ก์ ์์ ์ ๋ณต"
console.log(title);
}await์ ๋์ ์๋ฆฌ
await์ "์ ์๋ง์, ์ด ์์
์ด ๋๋ ๋๊น์ง๋ง ๋ฉ์ถฐ์ฃผ์ธ์"๋ผ๊ณ ๋งํ๋ ๊ฒ๊ณผ ๊ฐ์ต๋๋ค. ํ์ง๋ง ์๋ฐ์คํฌ๋ฆฝํธ๋ ์ฌ์ง ์์ต๋๋ค. ์ด ํจ์๋ง ์ ์ ๋ฉ์ท์ ๋ฟ, ๋ค๋ฅธ ๊ธํ ์ผ๋ค์ ์ฒ๋ฆฌํ๋ฌ ๊ฐ์ฃ .
// async/await โ Promise ์๋ฒฝ ๋์
// Promise ์ฒด์ด๋ ๋ฒ์
function loadPost(id) {
return fetchPost(id)
.then((post) => fetchUser(post.authorId))
.then((author) => ({ post, author }));
}
// async/await ๋ฒ์ (์์ ํ ๋์ผํ ๋์)
async function loadPost(id) {
const post = await fetchPost(id);
const author = await fetchUser(post.authorId); // post.authorId ์์กด โ ์์ฐจ ํ์
return { post, author };
}
// ๋ ๋ค Promise๋ฅผ ๋ฐํํ๋ฉฐ ์์ ํ ๋์ผํ๊ฒ ๋์
loadPost(1).then(({ post, author }) => console.log(post, author));๐ก๏ธ 3. ์๋ฌ ์ฒ๋ฆฌ ํจํด
try/catch/finally
์๋ฌ ์ฒ๋ฆฌ๋ ์ ํ์ด ์๋ ํ์์ ๋๋ค. ์ํธ ๋ฆฌ๋ ๋์ด "์กํ์ง ์์ ์๋ฌ๋ ์ด๋์ ํฐ์ง์ง ๋ชจ๋ฅด๋ ์ํํญํ๊ณผ ๊ฐ๋ค"๊ณ ๊ฐ์กฐํ ๊ธฐ๋ณธ ํจํด์ ์ตํ๋ณด๊ฒ ์ต๋๋ค.
// โ
๊ธฐ๋ณธ ์๋ฌ ์ฒ๋ฆฌ
async function loadPostDetail(postId) {
try {
const post = await fetchPost(postId);
const comments = await fetchComments(postId);
return { post, comments };
} catch (err) {
// ์ด๋ await์์ ์๋ฌ๊ฐ ๋๋ ์ฌ๊ธฐ์ ์กํ
console.error("๊ฒ์๊ธ ๋ก๋ ์คํจ:", err.message);
throw err; // ํ์ํ๋ฉด ์๋ก ๋ค์ throw
} finally {
// ์ฑ๊ณต/์คํจ ๋ฌด๊ดํ๊ฒ ํญ์ ์คํ
setLoading(false);
}
}
// โ ํํ ์ค์ โ async ํจ์๋ฅผ .catch ์์ด ํธ์ถ
loadPostDetail(1); // Promise๊ฐ reject๋ผ๋ ์๋ฌด๋ ์ ์ก์ โ Unhandled Promise Rejection
// โ
ํธ์ถ ์ธก์์๋ ์๋ฌ ์ฒ๋ฆฌ ํ์
try {
const data = await loadPostDetail(1);
render(data);
} catch (err) {
showError(err.message);
}์๋ฌ ๋ํผ ํจํด (Go ์คํ์ผ)
์ํธ ๋ฆฌ๋ ๋์ด Go ์ธ์ด๋ฅผ ํ๋ค JS ํ์ ๊ฐ์ ธ์จ ํจํด์ด๋ค. ๋งค๋ฒ try/catch๋ฅผ ๋ฐ๋ณตํ๋ ๊ฒ ๋ฒ๊ฑฐ๋ก์ธ ๋, ์๋ฌ๋ฅผ ๊ฐ์ผ๋ก ์ฒ๋ฆฌํ๋ค. ์์ฒ ์ด๊ฐ ์ฒ์ ๋ณด๊ณ "์ด๊ฑฐ ์ข ๋ฏ์ ๋ฐ์?"๋ผ๊ณ ํ์ง๋ง, ๋ง์ ์จ๋ณด๋ ์๋ฌ ์ฒ๋ฆฌ๋ฅผ ๋น ๋จ๋ฆฌ๊ธฐ๊ฐ ์คํ๋ ค ๋ ์ด๋ ค์์ก๋ค.
// ๋ํผ ์ ํธ๋ฆฌํฐ
async function to(promise) {
try {
const data = await promise;
return [null, data]; // [์๋ฌ, ๋ฐ์ดํฐ]
} catch (err) {
return [err, null];
}
}
// ์ฌ์ฉ โ try/catch ์์ด ๊น๋ํ๊ฒ
async function loadPostDetail(postId) {
const [postErr, post] = await to(fetchPost(postId));
if (postErr) {
console.error("๊ฒ์๊ธ ์กฐํ ์คํจ:", postErr);
return null;
}
const [commentsErr, comments] = await to(fetchComments(postId));
if (commentsErr) {
// ๋๊ธ์ ์คํจํด๋ ๊ฒ์๊ธ์ ๋ณด์ฌ์ค ์ ์์
console.warn("๋๊ธ ์กฐํ ์คํจ, ๋น ๋ฐฐ์ด๋ก ๋์ฒด");
return { post, comments: [] };
}
return { post, comments };
}โก 4. ์ฑ๋ฅ ์ต์ ํ โ await ๋จ๋ฐ ๊ธ์ง
await์ ๊ธฐ๊ณ์ ์ผ๋ก ์ฌ์ฉํ๋ค ๋ณด๋ฉด, ๋์์ ์ฒ๋ฆฌํ ์ ์๋ ์ผ๋ค์ ๊ดํ ์ค ์ธ์ ๊ธฐ๋ค๋ฆฌ๊ฒ ๋ง๋๋ ์ค์๋ฅผ ๋ฒํ๊ณค ํฉ๋๋ค. ์์ฒ ์ด์ ์ฝ๋๊ฐ ์ํธ ๋ฆฌ๋ ๋์ ์๊ธธ์ ๊ฑฐ์ณ ์ด๋ป๊ฒ 3๋ฐฐ ๋นจ๋ผ์ก๋์ง ํ์ธํด ๋ณด์ธ์.
// โ ์์งํ ์ฝ๋ โ ์ง๋ ฌ ์คํ (์ด 3์ด+)
// ๐ฃ ์์ฒ ์ ์ฝ๋
async function loadDashboard(userId) {
const posts = await fetchPosts(userId); // 1์ด ๋๊ธฐ
const friends = await fetchFriends(userId); // 1์ด ๋๊ธฐ (posts์ ๋ฌด๊ดํ๋ฐ๋ ๊ธฐ๋ค๋ฆผ!)
const notifications = await fetchNotifications(userId); // 1์ด ๋๊ธฐ
return { posts, friends, notifications };
}
// โ
๋ณ๋ ฌ ์คํ โ Promise.all (์ด ~1์ด)
// ๐ฆ ์ํธ์ ๋ฆฌํฉํ ๋ง
async function loadDashboard(userId) {
const [posts, friends, notifications] = await Promise.all([
fetchPosts(userId),
fetchFriends(userId),
fetchNotifications(userId),
]);
// ์ธ ์์ฒญ์ด ๋์์ ์์๋์ด ๊ฐ์ฅ ๋๋ฆฐ ์์ฒญ ์๊ฐ๋งํผ๋ง ๋๊ธฐ
return { posts, friends, notifications };
}
// โ
์์กด๊ด๊ณ๊ฐ ์์ ๋ โ ๋ถ๋ถ์ ๋ณ๋ ฌ
async function loadPostWithRelated(postId) {
const post = await fetchPost(postId); // post.authorId๊ฐ ํ์ํ๋ฏ๋ก ๋จผ์
// post ์ป์ ๋ค, author์ comments๋ ์๋ก ๋
๋ฆฝ์ โ ๋์์
const [author, comments] = await Promise.all([
fetchUser(post.authorId),
fetchComments(postId),
]);
return { post, author, comments };
}ํ๋จ ๊ธฐ์ค:
| ์ํฉ | ํจํด | ์ด์ |
|---|---|---|
| A, B, C๊ฐ ์๋ก ๋ ๋ฆฝ์ | Promise.all([A, B, C]) | ๋ณ๋ ฌ๋ก ์ต๋ ์ฑ๋ฅ |
| B๊ฐ A์ ๊ฒฐ๊ณผ์ ์์กด | await A โ await B | ์์๊ฐ ๊ฐ์ ๋จ |
| ์ผ๋ถ ์คํจ ํ์ฉ | Promise.allSettled([...]) | ๋ถ๋ถ ๊ฒฐ๊ณผ ํ์ฉ |
| ํ์์์ ํ์ | Promise.race([request, timeout]) | ์ ํ ์๊ฐ ์ค์ |
๐ 5. ์ค๋ฌด ํจํด ๋ชจ์
์ค๋ฌด์์ ์์ฃผ ๋ง๋๋ ์ํฉ๋ค์ ํจํด์ผ๋ก ์ ๋ฆฌํ์ต๋๋ค. ๋ฐ๋ณต๋ฌธ ์์์์ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋, ๋คํธ์ํฌ ๋ถ์์ ์ ๋๋นํ ์ฌ์๋ ๋ก์ง ๋ฑ์ ํ๋์ ์ดํด๋ณด์ธ์.
// ํจํด 1: ๋ฃจํ ๋ด ๋น๋๊ธฐ โ for...of vs map
const postIds = [1, 2, 3, 4, 5];
// โ forEach + async โ ๋์ ์ ํจ (forEach๋ Promise ๋ฌด์)
postIds.forEach(async (id) => {
const post = await fetchPost(id); // await์ด ๋ฌด์๋ฏธ
console.log(post);
});
// โ
for...of โ ์์ฐจ ์ฒ๋ฆฌ (์ ์๋ฃ ํ ๋ค์ ์์)
for (const id of postIds) {
const post = await fetchPost(id);
console.log(post);
}
// โ
map + Promise.all โ ๋ณ๋ ฌ ์ฒ๋ฆฌ (๋ชจ๋ ๋์ ์์)
const posts = await Promise.all(postIds.map((id) => fetchPost(id)));
// ํจํด 2: ์ฌ์๋ ๋ก์ง (Retry)
async function fetchWithRetry(url, maxRetries = 3) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await fetch(url).then((r) => r.json());
} catch (err) {
if (attempt === maxRetries) throw err;
await new Promise((resolve) => setTimeout(resolve, attempt * 1000)); // ์ง์ ๋ฐฑ์คํ
}
}
}
// ํจํด 3: ์ทจ์ ๊ฐ๋ฅํ ๋น๋๊ธฐ (AbortController)
async function fetchPostCancellable(postId, signal) {
const response = await fetch(`/api/posts/${postId}`, { signal });
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return response.json();
}
// ์ฌ์ฉ
const controller = new AbortController();
const postPromise = fetchPostCancellable(1, controller.signal);
// 3์ด ํ ์ทจ์
setTimeout(() => controller.abort(), 3000);
try {
const post = await postPromise;
} catch (err) {
if (err.name === "AbortError") console.log("์์ฒญ ์ทจ์๋จ");
}๐ ๋ง๋ฌด๋ฆฌ ํด์ฆ
Q1. ์๋ ์ฝ๋์์ ์ค์ ์คํ ์๊ฐ์ด ์ผ๋ง์ธ์ง, ๊ทธ๋ฆฌ๊ณ ์ด๋ป๊ฒ ์ต์ ํํ ์ ์๋์ง ์ค๋ช ํ๋ผ.
async function loadProfile(userId) {
const posts = await fetchPosts(userId); // 2์ด
const followers = await fetchFollowers(userId); // 1์ด
const following = await fetchFollowing(userId); // 1์ด
return { posts, followers, following };
}โ
์ ๋ต: ํ์ฌ ์ด 4์ด. Promise.all๋ก ๋ณ๋ ฌ ์ฒ๋ฆฌํ๋ฉด ์ต๋ 2์ด๋ก ๋จ์ถ.
๐ก ์์ธ ํด์ค:
- ์ธ API๊ฐ ์๋ก ์์กดํ์ง ์์ผ๋ฏ๋ก ์์ฐจ ์คํํ ์ด์ ๊ฐ ์๋ค.
- ์ต์ ํ:
async function loadProfile(userId) { const [posts, followers, following] = await Promise.all([ fetchPosts(userId), // ๋์ ์์ fetchFollowers(userId), // ๋์ ์์ fetchFollowing(userId), // ๋์ ์์ ]); return { posts, followers, following }; } // ์ด ๋๊ธฐ ์๊ฐ = ๊ฐ์ฅ ๊ธด ์์ฒญ ์๊ฐ = 2์ด - ๐ ํต์ฌ ๊ธฐ์ต๋ฒ: "
await์ด ์ฌ๋ฌ ๊ฐ ๋๋ํ ์์ผ๋ฉด ์์ฌํ๋ผ. ์์กด์ฑ์ด ์์ผ๋ฉดPromise.all๋ก ๋ฌถ์ด๋ผ."
Q2. async ํจ์์์ throwํ ์๋ฌ๋ ์ด๋ป๊ฒ ์ ํ๋๋๊ฐ?
โ
์ ๋ต: async ํจ์ ๋ด์์ throw๋ ์๋ฌ๋ ํด๋น ํจ์๊ฐ ๋ฐํํ๋ Promise๋ฅผ reject ์ํจ๋ค.
๐ก ์์ธ ํด์ค:
async function willFail() {
throw new Error("์คํจ!");
}
// willFail()์ด ๋ฐํํ๋ Promise๋ reject๋จ
willFail()
.then(() => console.log("์ฑ๊ณต")) // ์คํ ์ ๋จ
.catch((err) => console.error(err.message)); // "์คํจ!" โ
// async/await์ผ๋ก ์ก๊ธฐ
try {
await willFail();
} catch (err) {
console.error(err.message); // "์คํจ!" โ
}- ๐ ํต์ฌ ๊ธฐ์ต๋ฒ: "async ํจ์ ์์
throw= Promise์reject. async ํจ์๋ฅผ ํธ์ถํ๋ ์ชฝ์์ ๋ฐ๋์.catch๋๋try/catch๋ก ์ก์์ผ ํ๋ค."
Q3. ์์ฒ ์ด์ ํ ์คํธ ํ์ โ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํธ๋ ์ด๋์คํ ํ ํ์
ํ ํ์์์ ์์ PM์ด ๋งํ๋ค: "API ์์ฒญ ์ค์ ์ฌ์ฉ์๊ฐ ํ์ด์ง๋ฅผ ๋ ๋๋ฉด ์์ฒญ์ด ๊ณ์ ๋ ์๊ฐ๋ ๊ฑฐ ๋ง์ฃ ? ๊ทธ๊ฑฐ ์ทจ์ํ ์ ์๋์?"
์์ฒ ์ด๊ฐ
AbortController๋ฅผ ์ ์ํ๋ค. ๊ฐ ํ์์ ๊ด์ ์์ ์ด ๊ธฐ์ ์ ๋ช ๊ณผ ์์?
โ ์ ๋ต: AbortController๋ ๋ธ๋ผ์ฐ์ ๋ด์ฅ API๋ก ์ถ๊ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์ด ์์ฒญ ์ทจ์๊ฐ ๊ฐ๋ฅํ์ง๋ง, ์ง์ ๊ด๋ฆฌ๊ฐ ํ์ํ๊ณ Node.js ๊ตฌ๋ฒ์ ์์๋ ์ง์์ด ์ ํ๋๋ค.
๐ก ์์ธ ํด์ค:
- ๐ฆ ์ํธ (์๋ฆฌ): "AbortController๋ Fetch API์ ํตํฉ๋ ํ์ค ์ธํฐํ์ด์ค์ผ. signal์ ๋๊ธฐ๋ฉด abort() ํธ์ถ ์ fetch๊ฐ AbortError๋ฅผ throwํด. ์๋ฆฌ์ ๊นจ๋ํ๊ณ ํ์ค์ด์ผ."
- ๐ ์์ (์์ ์ฑ): "React Query๋ SWR ์ฐ๋ฉด ์ปดํฌ๋ํธ ์ธ๋ง์ดํธ ์ ์๋์ผ๋ก ์ทจ์ํด์ฃผ๊ฑฐ๋ . ์ง์ ๊ตฌํํ๋ฉด ๋๋ฝ ์ํ์ด ์์ด. ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ์ฉ์ ๊ฒํ ํ์."
- ๐จ ์์ (UX): "์ทจ์ ์ ๋๋ฉด ํ์ด์ง ์ ํ ํ์๋ ๋คํธ์ํฌ ํญ์ ์์ฒญ์ด ์์ด๊ณ , ๋ฆ๊ฒ ์จ ์๋ต์ด ํ์ฌ ํ์ด์ง์ ์ํฅ์ ์ค ์ ์์ด์. UX ๋ฒ๊ทธ์์."
- ๐ ํต์ฌ ๊ธฐ์ต๋ฒ: "async ์์ฒญ์ ๋ฐ๋์ ์ทจ์ ์๋๋ฆฌ์ค๋ฅผ ๊ณ ๋ คํ๋ผ. ๋ฆฌ์กํธ์์๋
useEffectcleanup์์controller.abort()๋ฅผ ํธ์ถํ๊ฑฐ๋, ๋ฐ์ดํฐ ํ์นญ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์๋ ์ฒ๋ฆฌํ๊ฒ ํ๋ผ."
๐ฃ ์์ฒ ์ด์ ํด๊ทผ ์ผ๊ธฐ
์ค๋์ ์ง์ง "์, ์ด๋์ ๊ทธ๋ฌ๊ตฌ๋!"์ ์ฐ์์ด์๋ค. await์ ๋ฌด์์ ์์ ๋ถ์ด๋ฉด ๋ค ๋๋ ์ค ์์๋๋ฐ, ์ง๋ ฌ๋ก ์คํ๋๋ค๋ ๊ฑธ ์๊ฒ ๋์ ๋ ์ข ๋จ๋ํ๋ค. ์ค์ ๋ก ๋ด๊ฐ ๋ง๋ ๋์๋ณด๋ ํ์ด์ง๊ฐ ์ ๋๋ ธ๋์ง ์ด์ ์ดํด๊ฐ ๋๋ค. ๋ค์ ์ฃผ์ Promise.all๋ก ๋ฆฌํฉํ ๋งํด๋ด์ผ์ง.
์ํธ ๋์ด "async ํจ์ ์์์ throwํ๋ฉด Promise reject์ผ"๋ผ๋ ๋ง์ด ๊ณ์ ๋จธ๋ฆฟ์์ ๋งด๋๋ค. ๋จ์ํ ๊ฒ ๊ฐ์๋ฐ ์ด๊ฒ ์๋ฌ ์ฒ๋ฆฌ์ ํต์ฌ์ด๋๋ผ.
๐ก ์ค๋์ ๊ตํ: "
await์ ์ฝ๊ธฐ ํธํ ์ฝ๋๋ฅผ ์ ์ฌํ์ง๋ง, ์๊ฐ ์์ด ์ฐ๋ฉด ์ฑ๋ฅ์ ๊ฐ์๋จน์ต๋๋ค. ์ฌ๋ฌ ๋น๋๊ธฐ ์์ ์ฌ์ด์ ์์กด์ฑ์ด ์๋ค๋ฉด,Promise.all๋ก ํ๊บผ๋ฒ์ ์ฒ๋ฆฌํ๋ ์ต๊ด์ ๋ค์ด์ธ์."
์ด๋ฒ ์ฃผ JS ๊ธฐ์ด ์คํฐ๋ ์ง์ง ์์ฐจ๋ค. ํด๊ทผ๊ธธ์ ์์ ๋ค์ผ๋ฉด์ ์ค๋ ๋ฐฐ์ด ๊ฒ๋ค ๋จธ๋ฆฟ์์ผ๋ก ํ ๋ฒ ๋ ์ ๋ฆฌํ๋ค. ๋ด์ผ ์ด๋ฒคํธ ๋ฃจํ ์ฑํฐ๋ผ๋๋ฐ โ ๋ค์ด๋ ๋ดค๋๋ฐ ์ ๋๋ก ์ดํดํ ์ ์ ์๋ ์ฃผ์ ๋ผ ๊ธฐ๋ ๋ฐ ๊ฑฑ์ ๋ฐ์ด๋ค.