03. async/await

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()
async.js μ˜ˆμ‹œ

Last updated

Was this helpful?