asyncë [TIL] 211012 ě°¸ęł
promise뼟 기ë¤ëŚŹę¸° ěí´ ěŹěŠëë¤
asyncę° ëśě í¨ě ěěěë§ ěŹěŠě´ ę°ëĽíë¤
promiseę° awaitě ë겨ě§ëŠ´
awaitě Promiseę° ěíě ěëŁí기뼟 기ë¤ë ¸ë¤ę°
í´ëš ę°ě 댏í´íë¤
function delay (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/* 1. asyncě await ě´ěŠí´ 'ë기ě ě¸ ě˝ë뼟 ěŹěŠíë ę˛ě˛ëź' ěěą */
async function getApple () { // (3) íëĄëŻ¸ě¤ëĽź ë§ë ë¤
await delay(3000); // (1) delay(3000)ę° ëë ëęšě§ 기ë¤ë ¸ë¤ę°
return 'đ'; // (2) ěŹęłźëĽź 댏í´íë
}
getApple()
.then(console.log); // 3ě´ ë¤ě đ ěśë Ľ
/* 2. ěëëźëŠ´ promise chainingě ě´ěŠí´ 'ëšë기ěěźëĄ' ěěą */
function getBanana() {
return delay(3000)
.then(() => 'đ');
}
getBanana()
.then(console.log); // 3ě´ ë¤ě đ ěśë Ľ
đĄ ěŹęłźě ë°ëëě ę°ě 모ë ę°ě ¸ě¤ë í¨ě뼟 ë§ë¤ě´ëł´ě.
ë´ę° ěěąí ëľě
console ě°˝ě ěśë Ľëě§ ěě
getBanana ě˝ë°ą í¨ě뼟 ě¤íí í ꡸ promiseě ę°ě return íě§ ěě기 ë돸
// ě¤ëľ
function pickFruits () {
return getApple()
.then(apple => getBanana(apple))
.then(banana => console.log(`${apple} + ${banana}`));
}
pickFruits();
ę°ě ëľě
return ěśę°
// ě ëľ
function pickFruits () {
return getApple()
.then(apple => {
return getBanana()
.then(banana => `${apple} + ${banana}`);
});
}
pickFruits().then(console.log);
đĄ asyncě awaitě ě´ěŠí´ ě˝ë°ą ě§ěĽ íěśí기
promiseę° awaitě ë겨ě§ëŠ´
awaitě Promiseę° ěíě ěëŁí기뼟 기ë¤ë ¸ë¤ę°
í´ëš ę°ě 댏í´íë¤
async function pickFruits () {
const apple = await getApple();
const banana = await getBanana();
return `${apple} + ${banana}`;
}
pickFruits().then(console.log);
ě´ě˛ëź asyncě awaitě ě´ěŠí´ ë기ě ěźëĄ ě¤íëë ě˝ë뼟 ěěąíëŻě´ ę°í¸íę˛ ěěąí ě ěë¤.
đĄ ěëŹ ě˛ëŚŹ
(Error Handling)
try...catch 돸
ě ěěąí´ ěëŹëĽź catch í ě ěëëĄ íë¤. try...catch 돸ě ě ě§ ěěźëŠ´, console ě°˝ě ë¤ěęłź ę°ě´ 'ěëŹ ë°ě!'ě ěĄě ě ěë¤ë ě° ěëŹę° ëŹë¤.(Uncaught (in promise) ěëŹ ë°ě!)async function getApple () {
await delay(3000);
throw 'ěëŹ ë°ě!';
return 'đ';
}
async function pickFruits () {
try {
const apple = await getApple();
const banana = await getBanana();
return `${apple} + ${banana}`;
} catch(error) {
console.log(error); // ěëŹ ë°ě!
}
}
pickFruits().then(console.log); // undefined
đĄ await ëłë Ź ě˛ëŚŹ
function delay (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// getApple í¨ěěě ěŹęłźëĽź ë°ěě¤ë ë° 3ě´ę° 깸댏ęł
async function getApple () {
await delay(3000);
return 'đ';
}
// getBanana() í¨ěěě ë°ëë뼟 ë°ěě¤ë ë°ë 3ě´ę° 깸댰ë¤
async function getBanana () {
await delay(3000);
return 'đ';
}
// ꡸ë°ë°
async function pickFruits () {
const apple = await getApple(); // (1) ěŹęłźëĽź ë°ěěŹ ëë awaitě ě¨ě 기ë¤ëŚŹëëĄ íęł
const banana = await getBanana(); // (2) ë°ëë뼟 ë°ěěŹ ëë awaitě ě¨ě 기ë¤ëŚŹëëĄ í늴
return `${apple} + ${banana}`; // (3)
}
// (1)ě´ ëëěźë§ (2)ę° ě§íëęł , (2)ę° ëëěźë§ (3)ě´ ě§íëëŻëĄ
// ěíë 결곟뼟 ěťę¸°ęšě§ë ě´ 6ě´ë 깸댰ë¤
// ěŹęłźě ë°ëë뼟 ë°ěě¤ë ě˝ëë ěě í ë
댽ě ě´ëŻëĄ, ě´ë ę˛ íë ę˛ě ěę° ëŠ´ěě ëší¨ě¨ě ě´ë¤
function delay (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function getApple () {
await delay(3000);
return 'đ';
}
async function getBanana () {
await delay(3000);
return 'đ';
}
async function pickFruits () {
// awaitě´ ěěźëŻëĄ getApple(), getBanana()ě 'ëłë Ź ě¤íě´ ę°ëĽíë¤.'
const applePromise = getApple(); // getApple í¨ěě ěí´ ěěąë promiseë, applePromise ëłěě í ëšëë¤.
const bananaPromise = getBanana(); // getBanana í¨ěě ěí´ ěěąë promiseë, bananaPromise ëłěě í ëšëë¤.
const apple = await applePromise;
const banana = await bananaPromise;
return `${apple} + ${banana}`;
}
đĽ ě˝ë ëśě (ě´í´ëĽź ěí´ apple ëśëśë§ ę°ě ¸ě´)
ě˝ę¸° ěě (1) â (2) â (3)const applePromise = getApple(); // (2) ꡸ë°ë° ě´ëŻ¸ (1) ě´ě ě promiseę° ěěąë ěę° // promise ěě ě˝ë ë¸ëě ë°ëĄ ě¤íë ěíě´ëŻëĄ const apple = await applePromise; // (1) applepromiseę° awaitě ë겨ě§ëŠ´ // awaitě applePromiseę° ěíě ěëŁí기뼟 기ë¤ë ¸ë¤ę° í´ëš ę°ě 댏í´íęł , // ě´ ę°ě´ apple ëłěě í ëšëë¤. // (3) ěŹę¸°ě applePromise ěě awaitě ě¨ě¤Źě´ë ěśę°ëĄ 기ë¤ëŚ´ íě ěě´ // ě´ëŻ¸ ě§í ě¤ě¸ applePromiseě ěíě´ ěëŁë늴 // applePromiseě resolveę° ě ëŹíë đę° ë°ëĄ apple ëłěě í ëšëë¤.
đĄ ꡸ëŹë ěě ěě ë ě˝ëë ěę° ëŠ´ěěë í¨ě¨ěąě´ ę°ě ëěěě§ ëŞ°ëźë, ěŹě í ëší¨ě¨ě ě´ë¤. ěëĄ ë 댽ě ěźëĄ ěí ę°ëĽí ě˝ëë¤ě promise.all()ě ě´ěŠí´ í츾 ę°ę˛°íę˛ ěěąí ě ěë¤.
Promise.all()
매ę°ëłěëĄ promise ë°°ě´ě ě ëŹí늴
모ë promiseë¤ě´ ëłë Źě ěźëĄ
ë¤ ë°ěě§ ëęšě§ 모ěě¤ë¤
function pickAllFruits () {
return Promise.all([getApple(), getBanana()])
.then(fruits => fruits.join(' + '));
}
pikAllFruits().then(console.log); // đ + đ
đĄ ě´ë¤ ęłźěźě´ë ěę´ěě´, 먟ě ë°ě§ë ęłźěź íëë§ě ë°ěě¤ęł ěśě 경ě°
Promise.race()
ë°°ě´ě ěěë¤(promiseë¤) ě¤ ę°ěĽ 먟ě ę°ě 댏í´íë promise
뼟 ë°ííë¤.
function delay (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function getApple () {
await delay(2000);
return 'đ';
}
async function getBanana () {
await delay(1000);
return 'đ';
}
function pickOnlyOne () {
return Promise.race([getApple(), getBanna()]);
}
pickOnlyOne().then(console.log); // đ
đĄ ęłźě : asyncě awaitě ě´ěŠí´ 기쥴 ě˝ë ë¤ě ěěą
â ě˝ë°ą ě§ěĽ â ⥠promise ě´ěŠí´ ě˝ë°ą ě§ěĽ íěśí기 â ⢠asyncě awaitě ě´ěŠí´ ě˝ë ę°ě
class UserStorage {
loginUser (id, password) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (id === 'syong' && password === 'happy' ||
id === 'corder' && password === 'nice') {
resolve(id);
} else {
reject(new Error('not found'));
}
}, 2000);
});
}
getRoles (user) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (user === 'syong') {
resolve({ name: 'syong', role: 'admin' });
} else {
reject(new Error('no access'));
}
}, 1000);
});
}
}
const userStorage = new UserStorage();
const id = prompt('id뼟 ě
ë Ľí´ěŁźě¸ě');
const password = prompt('password뼟 ě
ë Ľí´ěŁźě¸ě');
async function completeLogin () {
try {
const user = await userStorage.loginUser(id, password);
const nameAndRole = await userStorage.getRoles(user);
return `${nameAndRole.role} ${nameAndRole.name}ë, íěíŠëë¤!`
} catch(error) {
console.log(error);
}
}
completeLogin().then(alert);