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()

Last updated