Async-Await

uoayopยท2021๋…„ 2์›” 27์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
14/24
post-thumbnail

Javascript

Async-Await

๐Ÿ”ง ๋น„๋™๊ธฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

๐Ÿ”ง ํ•จ์ˆ˜ ์ด๋ฆ„ ์•ž์— async๋ฅผ ๋ถ™์ด๊ฑฐ๋‚˜, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์•ž์— async๋ฅผ ๋ถ™์ด๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

async function ํ•จ์ˆ˜์ด๋ฆ„() {  }
const ํ•จ์ˆ˜์ด๋ฆ„ = async() => { }

๐Ÿ”ง Async-Await ํ•จ์ˆ˜๋Š” Promise๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ณ ์žˆ๋‹ค.

  • ๋”ฐ๋ผ์„œ Promise๋ฅผ ๋จผ์ € ๊ณต๋ถ€ํ•˜๊ณ  ์˜ค๋ฉด ์ข‹๋‹ค.

Promise ๊ฐ์ฒด ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•

function p(ms){
    return new promise((resolve,reject)=>{
      setTimeout(()=>{
        resolve(ms);
      },ms);  
    });
}

//1. Promise ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ ๋น„๋™๊ธฐ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•  ๋•Œ

p(1000).then((ms) => {
    console.log(`${ms}ms ํ›„์— ์‹คํ–‰๋œ๋‹ค.`);
});

//2. Promise ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ await๋กœ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•

const ms = await p(1000);
console.log(`${ms}ms ํ›„์— ์‹คํ–‰๋œ๋‹ค.`);

// ๐Ÿšจ error : SyntaxError: await is only valid in async function
// await์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•ญ์ƒ async ํ•จ์ˆ˜ ์•ˆ์—์„œ ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•œ๋‹ค.

โญ๏ธ ํ•จ์ˆ˜ ์•ž์—๋Š” async, ํ”„๋กœ๋ฏธ์Šค ์•ž์—๋Š” await์„ ๋ถ™์—ฌ์ค€๋‹ค.


await๊ณผ async

  • async๊ฐ€ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๊ทธ ์•ˆ์— ์žˆ๋Š” ๋กœ์ง์ด ๋๋‚  ๋•Œ๊นŒ์ง€ ํ”„๋กœ๊ทธ๋žจ์ด ๋๋‚˜์ง€ ์•Š๋Š”๋‹ค.
  • await ํ•จ์ˆ˜๋ฅผ ๋งŒ๋‚˜๋ฉด ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ resolve๋œ ์ธ์ž๋ฅผ ๊ฐ€์ ธ์™€์„œ ๊ณ„์† ์ง„ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

โœจ await, async ์žฅ์  : ๋กœ์ง์˜ ์ฝ”๋“œ๊ฐ€ ๋ฐ‘์œผ๋กœ ๋‚ด๋ ค๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์–ด์„œ ์•Œ์•„๋ณด๊ธฐ ํŽธํ•˜๋‹ค.

function p(ms){
    return new Promise((resolve,reject)=>{
      setTimeout(()=>{
        resolve(ms);
      },ms);  
    });
}

//Promise ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ await๋กœ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•
(async function main(){
    const ms = await p(1000);
    console.log(`${ms}ms ํ›„์— ์‹คํ–‰๋œ๋‹ค.`);
})();

reject ๋œ ๊ฒฝ์šฐ try, catch ์ด์šฉํ•˜๊ธฐ

  • try๋ฌธ์œผ๋กœ ํ•จ์ˆ˜ p๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ๋งŒ์•ฝ reject ๋˜๋ฉด catch๋ฌธ์œผ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
function p(ms){
    return new Promise((resolve,reject)=>{
      setTimeout(()=>{
        reject(new Error('reason'));
      },ms);  
    });
}

(async function main(){
    try{
    const ms = await p(1000);
    } catch(error){
        console.log(error);
    }
})();

async function์—์„œ return ๋˜๋Š” ๊ฐ’

  • async function์—์„œ return ๋˜๋Š” ๊ฐ’์€ Promise.resolve ํ•จ์ˆ˜๋กœ ๊ฐ์‹ธ์ ธ์„œ ๋ฆฌํ„ด๋œ๋‹ค.
async function asyncP(){
    return 'doyeon';
}

(async function main(){
    try{
    const name = await asyncP();
    console.log(name);
    } catch(error){
        console.log(error);
    }
})();

function p(ms){
    return new Promise((resolve,reject)=>{
      setTimeout(()=>{
          resolve(ms);
        // reject(new Error('reason'));
      },ms);  
    });
}

async function asyncP(){
  	// ๋งŒ์•ฝ p์—์„œ reject๊ฐ€ ๋˜๋ฉด, asyncP์—์„œ try,catch๋ฌธ์œผ๋กœ ์—๋Ÿฌ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
    const ms = await p(1000);
    return 'doyeon:' + ms;
  	// 1์ดˆ ํ›„์— doyeon 1000 ์ด ์ถœ๋ ฅ๋œ๋‹ค.
}

(async function main(){
    try{
    const name = await asyncP();
    console.log(name);
    } catch(error){
        console.log(error);
    } finally {
        console.log('resolve ๋˜๊ฑฐ๋‚˜ reject ๋œ ํ›„์— end');
    }
})();

์—ฐ์†์ ์ธ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๋•Œ ์ฐจ์ด์ 

  • Promise : ๊ณ„์† chaining์„ ํ•ด์ค€๋‹ค. โ›“
  • Async : ํ•œ์ค„์”ฉ ๋๋‚ด๋ฉด์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๐Ÿ”šโณ
    • ์‚ฌ๋žŒ๋งˆ๋‹ค ๋‹ค๋ฅด๊ฒ ์ง€๋งŒ ๊ฐœ์ธ์ ์œผ๋กœ async๊ฐ€ ์ฝ”๋“œ์˜ ํ๋ฆ„์„ ๋” ์ง๊ด€์ ์œผ๋กœ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ์ข‹์€ ๊ฒƒ ๊ฐ™๋‹ค.
function p(ms){
    return new Promise((resolve,reject)=>{
      setTimeout(()=>{
        resolve(ms);
        //reject(new Error('reason'));
      },ms);  
    });
}

// Promise : ๊ณ„์† ๋งŒ๋“ค์–ด์„œ return, chaining
p(1000)
.then(()=> p(1000)
.then(()=> p(1000))
.then(()=>{
    console.log('3000ms ํ›„์— ๋ฐœ์ƒ');
}));

// Async : ํ•œ์ค„ํ•œ์ค„์ด ๋๋‚˜์•ผ ๋น„๋™๊ธฐ๊ฐ€ ์ง„ํ–‰๋จ
(async function main(){
    await p(1000);
    await p(1000);
    await p(1000);
    console.log('3000ms ํ›„์— ๋ฐœ์ƒ');
})();

Promise.all, Promise.race ๋ฅผ await, async๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ

function p(ms){
    return new Promise((resolve,reject)=>{
      setTimeout(()=>{
        resolve(ms);
        //reject(new Error('reason'));
      },ms);  
    });
}

(async function main() {
    const results = await Promise.all([p(1000),p(2000),p(3000)]);
    // ์ถœ๋ ฅ: 3์ดˆํ›„์— [1000,2000,3000]
    console.log(results);
})();

(async function main() {
    const result = await Promise.race([p(1000),p(2000),p(3000)]);
    // ์ถœ๋ ฅ: 1์ดˆํ›„์— 1000
    console.log(result);
})();
profile
slow and steady wins the race ๐Ÿข

0๊ฐœ์˜ ๋Œ“๊ธ€