async/await
async/await λ¬Έλ²μ ES8μ ν΄λΉνλ λ¬Έλ²μΌλ‘μ, Promise λ₯Ό λμ± μ½κ² μ¬μ© ν μ μκ² ν΄μ€λλ€.
κΈ°λ³Έμ μΈ μ¬μ©λ²μ μμλ΄
μλ€.
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function process() {
console.log('μλ
νμΈμ!');
await sleep(1000);
console.log('λ°κ°μ΅λλ€.!');
}
process();
// κ²°κ³Ό)
μλ
νμΈμ!
λ°κ°μ΅λλ€.!
async/await λ¬Έλ²μ μ¬μ©ν λμλ, ν¨μλ₯Ό μ μΈ ν λ ν¨μμ μλΆλΆμ async
ν€μλλ₯Ό λΆμ¬μ£ΌμΈμ. κ·Έλ¦¬κ³ Promise μ μλΆλΆμ await
μ λ£μ΄μ£Όλ©΄ ν΄λΉ νλ‘λ―Έμ€κ° λλ λκΉμ§ κΈ°λ€λ Έλ€κ° λ€μ μμ
μ μν ν μ μμ΅λλ€.
μ μ½λμμλ sleep
μ΄λΌλ ν¨μλ₯Ό λ§λ€μ΄μ νλΌλ―Έν°λ‘ λ£μ΄μ€ μκ° λ§νΌ κΈ°λ€λ¦¬λ Promise λ₯Ό λ§λ€κ³ , μ΄λ₯Ό process
ν¨μμμ μ¬μ©ν΄μ£Όμμ΅λλ€.
ν¨μμμ async
λ₯Ό μ¬μ©νλ©΄, ν΄λΉ ν¨μλ κ²°κ³Όκ°μΌλ‘ Promise λ₯Ό λ°ννκ² λ©λλ€. λ°λΌμ λ€μκ³Ό κ°μ΄ μ½λλ₯Ό μμ± ν μ μμ΅λλ€.
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function process() {
console.log('μλ
νμΈμ!');
await sleep(1000);
console.log('λ°κ°μ΅λλ€.!');
}
process().then(() => {
console.log('μμ
λλ¬μ΄μ!')
});
// κ²°κ³Ό)
μλ
νμΈμ!
λ°κ°μ΅λλ€.!
μμ
λλ¬μ΄μ!
async
ν¨μμμ μλ¬λ₯Ό λ°μ μν¬λμλ throw
λ₯Ό μ¬μ©νκ³ , μλ¬λ₯Ό μ‘μλΌ λμλ try/catch λ¬Έμ μ¬μ©ν©λλ€.
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function errorMake() {
await sleep(3000);
const error = new Error();
throw error;
};
async function process() {
try {
await errorMake();
} catch (e) {
console.log('then error : ', e) // μ¬κΈ°μ eλ μμ throw error;λ₯Ό κ°λ¦¬ν€κ² λ©λλ€.!!
}
}
process();
// κ²°κ³Ό)
then error : error
EX1)
new Promise
λ‘ μ μ νλ μ½λ
function a(value) {
return new Promise((resolve, reject) => {
setTimeout(function() { // μ¬κΈ°μ setTimeout νκ²μ μλ²μμ ν΅μ μμ μκ°μ κ±Έλ¦°λ€λ κ°μ ν΄μ μ΄λ―Έλ₯Ό setTimeoutν¨μλ₯Ό μ¬μ©νκ²μ
λλ€.
resolve(value)
},1000)
})
}
function b(value) {
return new Promise((resolve, reject) => {
setTimeout(function() { // μ¬κΈ°μ setTimeout νκ²μ μλ²μμ ν΅μ μμ μκ°μ κ±Έλ¦°λ€λ κ°μ ν΄μ μ΄λ―Έλ₯Ό setTimeoutν¨μλ₯Ό μ¬μ©νκ²μ
λλ€.
resolve(value)
},1000)
})
}
function c(value) {
return new Promise((resolve, reject) => {
setTimeout(function() { // μ¬κΈ°μ setTimeout νκ²μ μλ²μμ ν΅μ μμ μκ°μ κ±Έλ¦°λ€λ κ°μ ν΄μ μ΄λ―Έλ₯Ό setTimeoutν¨μλ₯Ό μ¬μ©νκ²μ
λλ€.
resolve(value)
},1000)
})
}
function d(value) {
return new Promise((resolve, reject) => {
setTimeout(function() { // μ¬κΈ°μ setTimeout νκ²μ μλ²μμ ν΅μ μμ μκ°μ κ±Έλ¦°λ€λ κ°μ ν΄μ μ΄λ―Έλ₯Ό setTimeoutν¨μλ₯Ό μ¬μ©νκ²μ
λλ€.
resolve(value)
},1000)
})
}
function test(value) {
return new Promise((resolve, reject) => {
if(false) { // μ μμ μΌλ‘ μλ²μ ν΅μ λͺ»νλ©΄ (μλ¬ λλ©΄) μ€νλ©λλ€.
reject('Error') //reject('μνλ λ©μΈμ§ μμ±')
}
setTimeout(() => { // μ¬κΈ°μ setTimeout νκ²μ μλ²μμ ν΅μ μμ μκ°μ κ±Έλ¦°λ€λ κ°μ ν΄μ μ΄λ―Έλ₯Ό setTimeoutν¨μλ₯Ό μ¬μ©νκ²μ
λλ€.
resolve(value)
}, 1000);
})
}
then catch μ€ννλ μ½λ
// then catch μ€ννλ μ½λ
test('A').then((res) => {console.log(res)})
.then(() => b("B").then((res) => {console.log(res)}))
.then(() => c("C").then((res) => {console.log(res)}))
.then(() => d("D").then((res) => {console.log(res)}))
.catch((error) => {
console.log(error)
alert(error)
})
.finally(() => {
console.log('done!!')
alert('done!!')
})
ES8λ²μ async await μ€ννλ μ½λ
// ES8λ²μ async await μ€ννλ μ½λ
async function asyncFunc() {
try {
const a0 = await test('A!')
console.log(a0)
const a1 = await a('A')
console.log(a1)
const b1 = await b('B')
console.log(b1)
const c1 = await c('C')
console.log(c1)
const d1 = await d('D')
console.log(d1)
} catch (error) {
alert(error)
console.log(error)
} finally {
console.log('done!!')
}
}
asyncFunc()