Error Handling
const turnonLaptop = () =>
new Promise((resolve, reject) => {
setTimeout(() => resolve(`๋
ธํธ๋ถ ์คํ.`), 1000);
});
const loginBlog = blog =>
new Promise((resolve, reject) => {
setTimeout(() => reject(new Error(`Error! ${blog} => ๋ธ๋ก๊ทธ ์ง์
.`)), 1000);
});
const writePost = post =>
new Promise ((resolve, reject) => {
setTimeout(() => resolve(`${post} => ๊ฒ์ ์๋ฃ.`), 1000);
});
producer ์ฝ๋๋ฅผ ์์ฑํ๋ค.
turnOnlaptop() //
.then(openBlog)
.then(writePost)
.then(console.log);
// VM86:7 Uncaught (in promise) Error: Error! ๋
ธํธ๋ถ ์คํ. => ๋ธ๋ก๊ทธ ์ง์
.
then๋ง ๋ถ์ด๋ฉด ์ด๋ ๊ฒ openBlog์์ reject๋ ์๋ฌ๋ฅผ catchํ์ง ๋ชปํด uncaught Error๊ฐ ๋๋ค.
turnonLaptop() //
.then(loginBlog)
.catch(error => {
return `๋น๋ฐ๋ฒํธ ์ฐพ๊ธฐ`;
})
.then(writePost)
.then(console.log)
.catch(console.log);
// ๋น๋ฐ๋ฒํธ ์ฐพ๊ธฐ => ๊ฒ์ ์๋ฃ.
catch๋ฅผ ์ฌ์ฉํ๋ฉด ๋ด๊ฐ ์ํ๋ ๊ณณ์์ error๋ฅผ handlingํ ์ ์๋ค.
์ถ์ฒ : ๋๋ฆผ์ฝ๋ฉ https://www.youtube.com/@dream-coding / MDN https://developer.mozilla.org/ko/