
Async / Await
Interested in ReactJS, RxJS and ReasonML.
ES2017(ECMA-262)μμ μλ‘κ² μΆκ°λ async/awaitλ λΉλκΈ° ν¨μλ₯Ό μ²λ¦¬νκΈ° μν ν€μλμ΄λ€.
μ²μ μ μλ κ²μ TypeScript 1.7μλ€.
λν, async/awaitλ Promiseμμ ꡬμΆλμκ³ , κΈ°μ‘΄μ λͺ¨λ νλ‘λ―Έμ€ κΈ°λ° APIμ νΈνλκΈ° λλ¬Έμ νλ‘λ―Έμ€μ κ°μ΄ μ¬μ©νκ±°λ, νλ‘λ―Έμ€λ₯Ό λ체ν μ μλ€.
λΉλκΈ° λμμ μν λκΈ°μ ꡬ문μ μ 곡ν¨μΌλ‘μ¨ μ½λ κ°λ
μ±μ λμΌ μ μμΌλ©°, μλ¬ νΈλ€λ§μ μν΄ try/catchλ₯Ό μ¬μ©νλ€.
μμνκΈ° μ μ
asyncμ awaitλ ν€μλμ΄λ―λ‘ polyfillμ΄ μμΌλ©°, μ΄μ λΈλΌμ°μ λ₯Ό μ§μνλ €λ©΄, babel λ± νΈλμ€νμΌλ¬λ₯Ό μ¬μ©ν΄μΌ νλ€.
μ¬μ©νλ λΈλΌμ°μ κ° async, awaitλ₯Ό μ§μνλμ§ νμΈνλ €λ©΄, Can I use?μμ ν΄λΉ ν€μλλ₯Ό κ²μνλ©΄ λλ€.
μμ μ½λλ₯Ό ν΅ν΄ async/await μ΄ν΄λ³΄κΈ°
function willBeAwaited() {
return new Promise(function (resolve) {
setTimeout(resolve, 400, 42);
});
}
async function asyncFunc () {
const resolvedValue = await willBeAwaited();
console.log(resolvedValue);
return resolvedValue + 500;
}
asyncFunc()
.then(function (resolvedValue) {
console.log(resolvedValue);
});
// 42
// 542asyncν€μλλ μΌλ°μ μΈ ν¨μλ₯Ό μλμΌλ‘ νλ‘λ―Έμ€ ννλ‘ λ§λ€λ©°, asyncν¨μλ₯Ό νΈμΆνλ©΄ ν΄λΉ ν¨μκ° λ¬΄μμ λ°ννλ resolveλλ€. λν, async ν¨μλ awaitμ μ¬μ©μ κ°λ₯νκ² νλ€.
awaitν€μλλ νλ‘λ―Έμ€ κ°μ²΄μ μλνλ©°, ν¨μμ νΈμΆ μμ μ¬μ©λλ©΄ νλ‘λ―Έμ€κ° ν΄κ²°λ λκΉμ§ μ½λ μ€νμ μ€λ¨νκ² νλ€. awaitλ μ€μ§ asyncν¨μμ μ¬μ©λ μ μλ€.
asyncFuncλ₯Ό μΌλ° νλ‘λ―Έμ€ μ€νμΌλ‘ μ¬μμ±νλ©΄, λ€μκ³Ό κ°λ€.
function asyncFunc() {
return willBeAwaited()
.then((resolvedValue) => {
console.log(resolvedValue);
return resolvedValue + 500;
});
}μλ¬ νΈλ€λ§
νλ‘λ―Έμ€λ ν΄κ²°(resolve)λ μλ μκ³ , κ±°μ (reject)λ μ μμΌλ©°, κ±°μ λλ€λ©΄ νλ‘λ―Έμ€ κ°μ²΄μ Promise.prototype.catchλ©μλλ₯Ό ν΅ν΄ μ²λ¦¬νλ€.
async/awaitμ κ²½μ°μλ try { ... } catch { ... }λ₯Ό μ¬μ©νλ€.
function willBeAwaited() {
return new Promise(function (resolve, reject) {
setTimeout(reject, 400, 'Error occured!');
});
}
async function asyncFunc () {
try {
const resolvedValue = await willBeAwaited();
console.log(resolvedValue);
return resolvedValue + 500;
} catch (e) {
console.log(e);
return 'Error occured when \'willBeAwaited\' is executed';
}
}
asyncFunc()
.then(function (resolvedValue) {
console.log(resolvedValue);
});
// Error occured!
// Error occured when 'willBeAwaited' is executedλ§μ½, async ν¨μμμ κ±°μ λ νλ‘λ―Έμ€λ₯Ό λ°ννλ €λ©΄ throwλ₯Ό ν΅ν΄ μλ¬λ₯Ό λμ§λ©΄ λλ€.
function willBeAwaited() {
return new Promise(function (resolve, reject) {
setTimeout(reject, 400, 'Error occured!');
});
}
async function asyncFunc () {
try {
const resolvedValue = await willBeAwaited();
console.log(resolvedValue);
return resolvedValue + 500;
} catch (e) {
console.log(e);
throw new Error(e);
}
}
asyncFunc()
.then(function (resolvedValue) {
console.log(resolvedValue);
})
.catch(function (e) {
console.log(e);
});
// Error occured!
// Error occured!async/awaitμ νκ³
1. asyncκ° μμ΄λ awaitλ₯Ό μ¬μ©ν μ μλ€.
μ μ μ€μ½νλ μΌλ° ν¨μ μ€μ½ν μμμ awaitλ₯Ό μ§μ μ¬μ©ν μ μμμ μλ―Ένλ€.
async function one() {
return 1;
}
// const num = await one();
// can not be executed
async function main() {
const num = await one();
console.log(num);
}
main();2. Promise.allκ³Ό κ°μ κ²½μ°λ₯Ό λ체ν μ μλ€.
λͺ¨λ λΉλκΈ° νΈμΆμ΄ λλ λκΉμ§ κΈ°λ€λ €μΌ νλ κ²½μ°, Promise.allμ μ¬μ©νλ κ²μ΄ ν¨μ¬ μ’λ€.
function sleep(ms = 0) {
return new Promise(resolve => setTimeout(function () {
resolve(ms);
}, ms));
}
(async function awaitAyncFunctions() {
const start = Date.now();
await sleep(1000);
await sleep(3000);
await sleep(2000);
const end = Date.now();
console.log(`execution time is ${Math.round((end - start) / 1000)}seconds.`);
// execution time is 6seconds.
})();
(async function awaitAyncFunctions() {
const start = Date.now();
const results = await
Promise.all([sleep(1000), sleep(3000), sleep(2000)]);
const end = Date.now();
console.log(`execution time is ${Math.round((end - start) / 1000)}seconds.`);
// execution time is 3seconds.
})();