JavaScript Promise
Promise
promise: ๋น๋๊ธฐ๋ฅผ ๊ฐํธํ๊ฒ ์ฒ๋ฆฌ ํ ์ ์๊ฒ ํด์ฃผ๋ object
์ ํด์ง ์ฅ์๊ฐ์ ๊ธฐ๋ฅ์ ์ํํ๊ณ ์ฑ๊ณตํด์ง ๋ฉ์ธ์ง์ ํจ๊ป ๊ธฐ๋ฅ ์ํ ์๋๋ฉด ์๋ฌ๊ฐ ๋ธ
Producer
// Promise is a JavaScript object for asynchronous peration
// state (์ํ):pending -> fulfilled or rejected
// producer vs cosumer ๋ง๋๋์ฌ๋/ ์๋นํ๋์ฌ๋
const promise = new Promise((resolve, reject) => {
// when new Promise is created, the ececutor runs automatically.
//doing some heavy work (network, read files)
// ์ฝ์ด์ค๋๊ฒ, ๋คํธ์ํฌ ์ด๋ฐ๊ฒ๋ค์ ๋ฌด๊ฒ๊ธฐ๋๋ฌธ์ ๋น๋๊ธฐ๋ก ์ฒ๋ฆฌํ๋๊ฒ์ด ์ข๋ค
// promise๊ฐ ๋ง๋ค์ด์ง ์๊ฐ ๋ฐ๋ก ์ํ์ ํ๊ฒ๋จ
// ์ฌ์ฉ์๊ฐ ๋ฒํผ์ ๋๋ ์๋ ๋คํธ์ํฌ๊ฐ ์๋๋๊ฒ ํ๋๊ฒ์ ๋ฐ๋ก ์ํ๋๊ธฐ๋๋ฌธ์ ๋ง์ง์์
console.log('doing something...');
setTimeout(() => {
resolve('ellie')
//reject(new Error('no network'));
}, 2000);
});consumers : then, catch, finally
promise chaining
Error Handling
์ฝ๋ฐฑ์ง์ฅ ์ฝ๋์์
Last updated
Was this helpful?