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
// ์ฝ์๋ ๊ฐ์ ์ ๋ฐ์์จ๋ค๋ฉด ์ ๋ฌ๋์ ๊ฒฐ๊ณผ๋ฌผ์ ์ถ๋ ฅ์ํด
// value์ ๊ฐ์ ์์ ์ฒ๋ฆฌ๊ณผ์ ์ด ์ ์งํ๋ ellie๋ผ๊ณ ์ฒ๋ฆฌ๋ ๊ฐ์ด ๋ค์ด์ค๊ฒ ๋จ
//than์ promise๋ก ๋ฆฌํด๋๊ณ ๋ค์ catch ๋ฅผ ๋ถ์ด๊ฒ ๋๋ค.
promise.then((value) => {
console.log(value);
})
.catch(error => {
console.log(error);
})
.finally(() => {
console.log('finally'); //์ฑ๊ณตํ๋ ์คํจํ๋ ์๊ด์์ด ์ด๋ค๊ธฐ๋ฅ์ ๋ง์ง๋ง์ผ๋ก ์ํํ๊ณ ์ถ์๋ finally๋ฅผ ์ฌ์ฉํจ
});
promise chaining
const fetchNumber = new Promise((resolve, reject) => {
setTimeout(() => resolve(1), 1000); // Promise ๋ 1์ด ์๋ค๊ฐ ์ซ์ 1์ ์ ๋ฌ
});
fetchNumber
.then(num => num * 2) // 2
.then(num => num * 3) //6
.then(num => {
return new Promise((resolve, reject) => { // ์๋ก์ด Promise๋ฅผ ๋ฆฌํด
setTimeout(() => resolve(num - 1), 1000); //5
});
})
.then(num => console.log(num)); //5
Error Handling
// ์๋ญ์ ๋ฐ์์ค๋
const getHen = () =>
new Promise((resolve, reject) => {
setTimeout(() => resolve('๐'), 1000);
});
// ์๋ญ์ ๋ฐ์์ ๋ญ์ผ๋ก๋ถํฐ ๋ฌ๊ฑ์ ์ป์ด์ค๋
const getEgg = hen =>
new Promise((resolve, reject) => {
setTimeout(() => reject(new Error(`ERROR! ${hen} => ๐ฅ`)), 1000);
});
// ๋ฌ๊ฑ์ ๋ฐ์์์ ์๋ฆฌํ๋๊ฒ
const cook = egg =>
new Promise((resolve, reject) => {
setTimeout(() => resolve(`${egg} => ๐ณ`), 1000);
});
// ๊ณ๋์ ๋ฐ์์ค๋๊ฒ์ ๋ฌธ์ ๊ฐ ์๊ฒจ๋ ๋ค๋ฅธ๊ฒ์ผ๋ก ๋์ฒดํ๋๊ฒ
// ํด๋น๋ถ๋ถ์์ ๋ฌธ์ ๊ฐ ์๊ธดํ์๋ catch๋ฅผ ์ด์ฉํด error๋ฅผ ์ฒ๋ฆฌํ๊ณ ๊ธฐ๋ฅ์ ์งํ ํ ์ ์๋ค.
getHen()
.then(getEgg)
.catch(error => { // ์๋ฌ ํธ๋ค๋ง ๊ณ๋์ ๋ฐ์๋ ๋ฌธ์ ๊ฐ ์๊ธด๋ค๋ฉด -> ๋ค๋ฅธ๊ฒ์ ๋ฆฌํดํด์ค
return '๐ฅ';
})
.then(cook)
.then(console.log)
.catch(console.log); // ์์ ํธ๋ค๋ง์ด ์์ผ๋ฉด ์๋ฌ ๋ฉ์ธ์ง(ERROR! ๐ => ๐ฅ)๊ฐ ๋ธ
์ฝ๋ฐฑ์ง์ฅ ์ฝ๋์์
class UserStorage {
loginUser(id, password){
return new Promise((resolve, reject) =>
setTimeout(() => {
if (
(id === 'ellie' && password === 'dream') ||
(id === 'coder' && password === 'academy')
) {
resolve(id);
} else{
reject(new Error('not found')); // ์๋ฌ 'not found'
}
}, 2000);
});
// ์ฌ์ฉ์๊ฐ ์๋ฆฌ์ด๋ฉด ์ด๋ฆ์ ์๋ฆฌ๊ณ role ์ ์ด๋๋ฏผ์ธ ๊ฐ์ ์ ๋ฌํด์ฃผ๊ณ ์๋๋ฉด not access๋ผ๊ณ ์ ๋ฌ
getRoles(user) {
return new Promise((resolve, reject) => {
if (user === 'ellie') {
resolve({ name: 'ellie', role: 'admin' });
} else {
reject(new Error('no access')); // ์๋ฌ 'not access'
}
}, 1000);
});
}
}
// ๋ณ์๋ ๋ณดํต ์๋ฌธ์๋ก ์์ํ๋ค ๋์ผํ์ด๋ฆ์ผ๋ก ๋ณ์ ์ ์ธํ์ง ๋ง๊ฒ
const userStorage = new UserStorage();
const id = prompt('enter your id'); // ์์ด๋ ๋ฐ์์ค๊ธฐ
const password = prompt('enter your password');
userStorage
.loginUser(id, password)
.then(userStorage.getRoles)
.then(user => alert(`hello ${userWithRole.name}, you have a ${userWithRole.role} role`));
.catch(console.log);
Last updated
Was this helpful?