๋น๋๊ธฐ ์ฒ๋ฆฌ ๋ฐฉ์ ์ค ํ๋์ด๋ค.
๋ฌธ์ ์ : Callback hell ๋น๋๊ธฐ ์ฒ๋ฆฌ ๋ก์ง์ ์ํด ์ฝ๋ฐฑ ํจ์๋ฅผ ์ฐ์ํด์ ์ฌ์ฉํ ๋ ๋ฐ์ํ๋ ๋ฌธ์ ์ด๋ค. ์ฝ๋ฐฑ์์ ์ฝ๋ฐฑ์ ๊ณ์ ๋ฌด๋ ํ์์ผ๋ก ์ด๋ฌํ ์ฝ๋ ๊ตฌ์กฐ๋ ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๊ณ ๋ก์ง์ ๋ณ๊ฒฝํ๊ธฐ ์ด๋ ต๋ค.
ํด๊ฒฐ๋ฐฉ๋ฒ : Promise๋ Async๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
Callback example
const printString = (string) => {
setTimeout(() => {
console.log(string)
},
Math.floor(Math.random() * 100) + 1
)
}
const printAll = () => {
printString("A")
printString("B")
printString("C")
}
printAll()
// ๋ฌด์์๋ก ์ถ๋ ฅ
// callback์ ์ด์ฉํ ์ถ๋ ฅ
const printString = (string, callback) => {
setTimeout(() => {
console.log(string)
callback()
},
Math.floor(Math.random() * 100) + 1
)
}
const printAll = () => {
printString("A", () => {
printString("B", () => {
printString("C", () => {})
})
})
}
printAll()
// A, B, C ์์๋ก ์ถ๋ ฅ
ํ๋ก๋ฏธ์ค๋ ์๋ฐ์คํฌ๋ฆฝํธ ๋น๋๊ธฐ ์ฒ๋ฆฌ์ ์ฌ์ฉ๋๋ ๊ฐ์ฒด์ด๋ค.
์๋ฐ์คํฌ๋ฆฝํธ์์ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ ? ํน์ ์ฝ๋์ ์คํ์ด ์๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ๋ค์ ์ฝ๋๋ฅผ ๋จผ์ ์ํํ๋ ์๋ฐ์คํฌ๋ฆฝํธ์ ํน์ฑ
Promise์ 3๊ฐ์ง ์ํ
: Promise๋ฅผ ์ฌ์ฉํ ๋ ์์์ผ ํ๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๊ฐ๋
์ด ๋ฐ๋ก Promise์ ์ํ์ด๋ค. ์ด๋ Promise์ ์ฒ๋ฆฌ ๊ณผ์ ์ ์๋ฏธํ๋ค. new Promise()๋ก Promise๋ฅผ ์์ฑํ๊ณ ์ข
๋ฃ๋ ๋๊น์ง 3๊ฐ์ง ์ํ๋ฅผ ๊ฐ์ง๋ค.
Pending(๋๊ธฐ) : ๋น๋๊ธฐ ์ฒ๋ฆฌ ๋ก์ง์ด ์์ง ์๋ฃ๋์ง ์์ ์ํ
new Promise(function (resolve, reject) {
// ...
});
: ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ๋๊ธฐ ์ํ๊ฐ ๋๊ณ , ํธ์ถํ ๋ ์ฝ๋ฐฑ ํจ์์ ์ธ์๋ก resolve, reject์ ์ ๊ทผํ ์ ์๋ค.
Fulfilled(์ดํ) : ๋น๋๊ธฐ ์ฒ๋ฆฌ๊ฐ ์๋ฃ๋์ด Promise๊ฐ ๊ฒฐ๊ณผ ๊ฐ์ ๋ฐํํด์ค ์ํ
function getData() {
return new Promise(function (resolve, reject) {
var data = 100;
resolve(data);
});
}
// resolve()์ ๊ฒฐ๊ณผ ๊ฐ data๋ฅผ resolvedData๋ก ๋ฐ์
getData().then(function (resolvedData) {
console.log(resolvedData); // 100
});
: resolve๋ฅผ ์คํํ๋ฉด ์ดํ ์ํ๊ฐ ๋๋ค. ์ดํ then()์ ์ด์ฉํ์ฌ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ ๊ฐ์ ๋ฐ์ ์ ์๋ค.
Rejected(์คํจ) : ๋น๋๊ธฐ ์ฒ๋ฆฌ๊ฐ ์คํจํ๊ฑฐ๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ ์ํ
function getData() {
return new Promise(function (resolve, reject) {
reject(new Error("Request is failed"));
});
}
// reject()์ ๊ฒฐ๊ณผ ๊ฐ Error๋ฅผ err์ ๋ฐ์
getData().then().catch(function (err) {
console.log(err); // Error: Request is failed
});
: reject() ๋ฉ์๋๋ฅผ ์คํํ๋ฉด ์คํจ ์ํ๊ฐ ๋๋ค. ์คํจ์ํ๊ฐ ๋๋ฉด ์คํจํ ์ด์ (์คํจ ์ฒ๋ฆฌ์ ๊ฒฐ๊ณผ ๊ฐ)์ catch()๋ก ๋ฐ์ ์ ์๋ค.
// example
function getData() {
return new Promise(function (resolve, reject) {
$.get('url ์ฃผ์/products/1', function (response) {
if (response) {
resolve(response);
}
reject(new Error("Request is failed"));
});
});
}
// Fulfilled ๋๋ Rejected์ ๊ฒฐ๊ณผ ๊ฐ ์ถ๋ ฅ
getData().then(function (data) {
console.log(data); // response ๊ฐ ์ถ๋ ฅ
}).catch(function (err) {
console.error(err); // Error ์ถ๋ ฅ
});
// promise ์ด์ฉ
const printString = (string) => {
// callback์ ์ธ์๋ก ๋ฐ์ง ์๋๋ค
// new Promise() ์ถ๊ฐ
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(string)
resolve()
},
Math.floor(Math.random() * 100) + 1
)
})
}
const printAll = () => {
printString("A")
.then(() => {
printString("B")
})
.then(() => {
printString("C")
})
// ๋ง์ฝ reject๊ฐ ์ฌ์ฉ๋๋ฉด, ์ด๊ณณ์ catch๋ฅผ ์ด์ฉํ์ฌ error ํ์
}
printAll()
function sitAndCode(){
return new Promise((resolve, reject) => {
setTimeout(() => { resolve('sit and code') }, 100)
})
}
function eatLunch(){
return new Promise((resolve, reject) => {
setTimeout(() => { resolve('eat Lunch') }, 100)
})
}
function goToBed(){
return new Promise((resolve, reject) => {
setTimeout(() => { resolve('go To Bed') }, 100)
})
}
sitAndCode()
.then(() => {
return eatLunch
})
.then(() => {
return goToBed
})
๊ธฐ์กด ๋น๋๊ธฐ ์ฒ๋ฆฌ ๋ฐฉ์์ธ Callback & Promise์ ๋จ์ ์ ๋ณด์ํ๊ณ ๊ฐ๋ฐ์๊ฐ ์ฝ๊ธฐ ์ข์ ์ฝ๋๋ก ์์ฑ ๊ฐ๋ฅ
// ๊ธฐ๋ณธ ๋ฌธ๋ฒ
async function ํจ์๋ช
() {
await ๋น๋๊ธฐ_์ฒ๋ฆฌ_๋ฉ์๋_๋ช
();
}
: ํจ์ ์์ async ๋ผ๋ ์์ฝ์ด๋ฅผ ๋ถ์ธ๋ค. ๊ทธ๋ฆฌ๊ณ ํจ์ ๋ด๋ถ ๋ก์ง ์ค HTTP ํต์ ์ ํ๋ ๋น๋๊ธฐ ์ฒ๋ฆฌ ์ฝ๋ ์์ await๋ฅผ ๋ถ์ธ๋ค.
์ฌ๊ธฐ์ ์ค์ํ ๊ฒ์ ๋น๋๊ธฐ ์ฒ๋ฆฌ ๋ฉ์๋๊ฐ ๊ผญ Promise ๊ฐ์ฒด๋ฅผ ๋ฐํํด์ผ
await๊ฐ ์๋ํ ๋๋ก ๋์ํ๋ค๋ ์ฌ์ค
์ผ๋ฐ์ ์ผ๋กawait์ ๋์์ด ๋๋ ๋น๋๊ธฐ ์ฒ๋ฆฌ ์ฝ๋๋Axios๋ฑ Promise๋ฅผ ๋ฐํํ๋ API ํธ์ถ ํจ์
function sitAndCode(){
return new Promise((resolve, reject) => {
setTimeout(() => { resolve('sit and code') }, 100)
})
}
function eatLunch(){
return new Promise((resolve, reject) => {
setTimeout(() => { resolve('eat Lunch') }, 100)
})
}
function goToBed(){
return new Promise((resolve, reject) => {
setTimeout(() => { resolve('go To Bed') }, 100)
})
}
var result = async () => {
const one = await sitAndCode();
console.log(one);
const two = await eatLunch();
console.log(two);
const three = await goToBed();
console.log(three);
}
result();
์์ธ์ฒ๋ฆฌ
: try catch ๋ฐฉ๋ฒ
async function logTodoTitle() {
try {
var user = await fetchUser();
if (user.id === 1) {
var todo = await fetchTodo();
console.log(todo.title); // delectus aut autem
}
} catch (error) {
console.log(error);
}
}