μ½λ°± μ§μ₯(Callback Hell)μ λΉλκΈ° νλ‘κ·Έλλ°μμ μμ£Ό λ°μνλ μ½λ ꡬ쑰μ ν μ’
λ₯λ‘, μ¬λ¬ λΉλκΈ° λμμ΄ μ€μ²©λ ννλ‘ μ½λκ° μμ±λ λ λ°μνλ κ°λ
μ± μ νμ μ μ§λ³΄μμ μ΄λ €μμ μ§μΉν©λλ€. μ£Όλ‘ μ½λ°± ν¨μλ₯Ό μ¬μ©νλ μν©μμ λΉλκΈ°μ μΈ μμ
λ€μ΄ μ€μ²©λμ΄ λ°μνλ©°, μ½λκ° κΈΈμ΄μ§μλ‘ κ°λ
μ±μ΄ λ¨μ΄μ§κ³ λ‘μ§μ μ΄ν΄κ° μ΄λ €μμ§λ λ¬Έμ κ° λ°μν©λλ€.
μλ₯Ό λ€μ΄, μλ²μμ λ°μ΄ν°λ₯Ό λ°μμ€κ³ , κ·Έ λ°μ΄ν°λ₯Ό κ°κ³΅ν ν λ€μ μλ²μ μ μ‘νκ³ , κ·Έ μλ΅μ λ°μμμ νλ©΄μ μΆλ ₯νλ μΌλ ¨μ λΉλκΈ° λμλ€μ΄ μ€μ²©λλ μν©μμ μ½λ°± μ§μ₯μ΄ λ°μν μ μμ΅λλ€. μ΄λ¬ν μ€μ²©λ μ½λ°± ν¨μλ€μ μ½λμ λ€μ¬μ°κΈ° μμ€μ΄ κΉμ΄μ§κ³ , κ°λ μ±μ΄ λ¨μ΄μ Έ μ½λλ₯Ό μ΄ν΄νκΈ° μ΄λ €μμ§λλ€.
μλ₯Ό λ€μ΄, μλ²μμ λ°μ΄ν°λ₯Ό λ°μμ€κ³ , κ·Έ λ°μ΄ν°λ₯Ό κ°κ³΅ν ν λ€μ μλ²μ μ μ‘νκ³ , κ·Έ μλ΅μ λ°μμμ νλ©΄μ μΆλ ₯νλ μΌλ ¨μ λΉλκΈ° λμλ€μ΄ μ€μ²©λλ μν©μμ μ½λ°± μ§μ₯μ΄ λ°μν μ μμ΅λλ€. μ΄λ¬ν μ€μ²©λ μ½λ°± ν¨μλ€μ μ½λμ λ€μ¬μ°κΈ° μμ€μ΄ κΉμ΄μ§κ³ , κ°λ μ±μ΄ λ¨μ΄μ Έ μ½λλ₯Ό μ΄ν΄νκΈ° μ΄λ €μμ§λλ€.
asyncFunction1(function(result1) {
asyncFunction2(result1, function(result2) {
asyncFunction3(result2, function(result3) {
// ...
});
});
});μ΄λ¬ν μ€μ²©λ ꡬ쑰λ λΉλκΈ° λμμ΄ λ§μμ§μλ‘ μ½λλ₯Ό μ΄ν΄νκΈ° μ΄λ €μμ§λ©°, μλ¬ μ²λ¦¬ λ° μ μ§λ³΄μκ° λ³΅μ‘ν΄μ§λλ€.
μ½λ°± μ§μ₯μ ν΄κ²°νκΈ° μν λ°©λ²μΌλ‘λ Promiseλ Async/Awaitμ κ°μ λΉλκΈ° μ²λ¦¬ λ°©μμ νμ©νλ κ²μ΄ μμ΅λλ€. μ΄λ₯Ό ν΅ν΄ μ½λμ κ°λ μ±μ ν₯μμν€κ³ , λΉλκΈ° λμλ€μ λ³΄λ€ λͺ μμ μ΄κ³ κ΄λ¦¬νκΈ° μ¬μ΄ ννλ‘ κ΅¬νν μ μμ΅λλ€.
μ½λ°± μ§μ₯μ ν΄κ²°νκΈ° μν λ°©λ²μΌλ‘λ Promiseλ Async/Awaitμ κ°μ λΉλκΈ° μ²λ¦¬ λ°©μμ νμ©νλ κ²μ΄ μμ΅λλ€. μ΄λ₯Ό ν΅ν΄ μ½λμ κ°λ μ±μ ν₯μμν€κ³ , λΉλκΈ° λμλ€μ λ³΄λ€ λͺ μμ μ΄κ³ κ΄λ¦¬νκΈ° μ¬μ΄ ννλ‘ κ΅¬νν μ μμ΅λλ€.
Promiseλ Async/Awaitμ μ¬μ©νμ¬ μ½λ°±μ§μ₯μ ν΄κ²°νλ λ°©λ²
μ½λ°± μ§μ₯μ ν΄κ²°νκΈ° μν΄ Promiseλ Async/Awaitμ μ¬μ©νλ κ²μ λΉλκΈ° μ½λλ₯Ό λ κ°κ²°νκ³ κ°λ
μ± μκ² λ§λ€ μ μλ λ°©λ² μ€ νλμ
λλ€. μλλ κ°κ° Promiseμ Async/Awaitμ μ¬μ©νμ¬ μ½λ°± μ§μ₯μ ν΄κ²°νλ μμ μ½λμ
λλ€.
π 1. Promise μ¬μ©
function asyncFunction1() {
return new Promise((resolve) => {
// λΉλκΈ° μμ
μν
setTimeout(() => {
console.log('Async Function 1');
resolve('Result from Async Function 1');
}, 1000);
});
}
function asyncFunction2(result) {
return new Promise((resolve) => {
// λΉλκΈ° μμ
μν
setTimeout(() => {
console.log('Async Function 2');
resolve('Result from Async Function 2');
}, 1000);
});
}
function asyncFunction3(result) {
return new Promise((resolve) => {
// λΉλκΈ° μμ
μν
setTimeout(() => {
console.log('Async Function 3');
resolve('Result from Async Function 3');
}, 1000);
});
}
asyncFunction1()
.then((result1) => asyncFunction2(result1))
.then((result2) => asyncFunction3(result2))
.then((result3) => {
console.log('Final Result:', result3);
})
.catch((error) => {
console.error('Error:', error);
});
π Async/Await μ¬μ©
function asyncFunction1() {
return new Promise((resolve) => {
// λΉλκΈ° μμ
μν
setTimeout(() => {
console.log('Async Function 1');
resolve('Result from Async Function 1');
}, 1000);
});
}
function asyncFunction2(result) {
return new Promise((resolve) => {
// λΉλκΈ° μμ
μν
setTimeout(() => {
console.log('Async Function 2');
resolve('Result from Async Function 2');
}, 1000);
});
}
function asyncFunction3(result) {
return new Promise((resolve) => {
// λΉλκΈ° μμ
μν
setTimeout(() => {
console.log('Async Function 3');
resolve('Result from Async Function 3');
}, 1000);
});
}
async function runAsyncFunctions() {
try {
const result1 = await asyncFunction1();
const result2 = await asyncFunction2(result1);
const result3 = await asyncFunction3(result2);
console.log('Final Result:', result3);
} catch (error) {
console.error('Error:', error);
}
}
runAsyncFunctions();
μμ μ½λμμλ κ°κ°μ λΉλκΈ° ν¨μκ° Promiseλ₯Ό λ°ννλλ‘ μμ±λμκ³ , μ΄λ₯Ό μμ°¨μ μΌλ‘ νΈμΆνλλ‘ then λλ awaitμ μ¬μ©νμμ΅λλ€. async & awaitμμ μμΈλ₯Ό μ²λ¦¬νλ λ°©λ²μ λ°λ‘ try catchμ
λλ€.
μ½λμμ λ°μν λ€νΈμν¬ ν΅μ μ€λ₯λΏλ§ μλλΌ κ°λ¨ν νμ μ€λ₯ λ±μ μΌλ°μ μΈ μ€λ₯κΉμ§λ catchλ‘ μ‘μλΌ μ μμ΅λλ€. λ°κ²¬λ μλ¬λ error κ°μ²΄μ λ΄κΈ°κΈ° λλ¬Έμ μλ¬μ μ νμ λ§κ² μλ¬ μ½λλ₯Ό μ²λ¦¬ν΄μ£Όμλ©΄ λ©λλ€.
νλ‘λ―Έμ€λ₯Ό μ¬μ©νκ² λλ©΄ νλ‘λ―Έμ€ κ°μ²΄μ λΉλκΈ° μ²λ¦¬λ κ²°κ³Όκ°μ΄ μ μ₯λλ©° μ½λ°±μ κ²½μ° λ§€λ² λΉλκΈ° ν¨μλ₯Ό νΈμΆνμ¬ λΉλκΈ°μ²λ¦¬λ κ²°κ³Όκ°μ μ»μλ€λ©΄ νλ‘λ―Έμ€λ .then λ©μλλ₯Ό ν΅ν΄μ μνλ λμ μ μ₯λμ΄ μλ λΉλκΈ° κ°μ μ¬μ©ν μ μμ΅λλ€.
Async & Awaitμ μ¬μ©νλ©΄ λΉλκΈ° μ½λλ₯Ό λκΈ° μ½λμ²λΌ μμ±ν μ μμ΅λλ€. μ΄λ‘μ¨ μ½λμ μλκ° λͺ νν΄μ§κ³ κ°λ μ±μ΄ ν₯μλ©λλ€. Async & Awaitμ μ¬μ©νλ©΄ μ½λ°± μ§μ₯μ νΌνκ³ λΉλκΈ° μ½λλ₯Ό ꡬ쑰μ μ΄κ³ λͺ λ£νκ² μμ±ν μ μμ΅λλ€.
μ½λμμ λ°μν λ€νΈμν¬ ν΅μ μ€λ₯λΏλ§ μλλΌ κ°λ¨ν νμ μ€λ₯ λ±μ μΌλ°μ μΈ μ€λ₯κΉμ§λ catchλ‘ μ‘μλΌ μ μμ΅λλ€. λ°κ²¬λ μλ¬λ error κ°μ²΄μ λ΄κΈ°κΈ° λλ¬Έμ μλ¬μ μ νμ λ§κ² μλ¬ μ½λλ₯Ό μ²λ¦¬ν΄μ£Όμλ©΄ λ©λλ€.
νλ‘λ―Έμ€λ₯Ό μ¬μ©νκ² λλ©΄ νλ‘λ―Έμ€ κ°μ²΄μ λΉλκΈ° μ²λ¦¬λ κ²°κ³Όκ°μ΄ μ μ₯λλ©° μ½λ°±μ κ²½μ° λ§€λ² λΉλκΈ° ν¨μλ₯Ό νΈμΆνμ¬ λΉλκΈ°μ²λ¦¬λ κ²°κ³Όκ°μ μ»μλ€λ©΄ νλ‘λ―Έμ€λ .then λ©μλλ₯Ό ν΅ν΄μ μνλ λμ μ μ₯λμ΄ μλ λΉλκΈ° κ°μ μ¬μ©ν μ μμ΅λλ€.
Async & Awaitμ μ¬μ©νλ©΄ λΉλκΈ° μ½λλ₯Ό λκΈ° μ½λμ²λΌ μμ±ν μ μμ΅λλ€. μ΄λ‘μ¨ μ½λμ μλκ° λͺ νν΄μ§κ³ κ°λ μ±μ΄ ν₯μλ©λλ€. Async & Awaitμ μ¬μ©νλ©΄ μ½λ°± μ§μ₯μ νΌνκ³ λΉλκΈ° μ½λλ₯Ό ꡬ쑰μ μ΄κ³ λͺ λ£νκ² μμ±ν μ μμ΅λλ€.