๐Ÿ‘ async await ์‹ฌํ™”

KHWยท2021๋…„ 1์›” 26์ผ
0

Javascript ์ง€์‹์Œ“๊ธฐ

๋ชฉ๋ก ๋ณด๊ธฐ
18/95

4๊ฐ€์ง€์˜ async await ์‹ฌํ™”

  1. ๋ฐ”๋กœ์ง„ํ–‰
  2. 10์ดˆ ๋’ค ์ง„ํ–‰
  3. 5์ดˆ ๋’ค ์ง„ํ–‰
  4. 5์ดˆ ๋’ค ์ง„ํ–‰ (promise.all)

1. ๋ฐ”๋กœ์ง„ํ–‰

// ๋ฐ”๋กœ
function delay(ms){
  return new Promise(resolve=>{
    setTimeout(()=>{},ms)
    resolve();
  });
}

async function getApple(){
  await delay(5000);
  return 'apple';
}
async function getBanana(){
  await delay(5000);
  return 'banana';
}

async function pickFruits(){
  const a = await getApple();
  const b = await getBanana();
  return `${a} + ${b}`;
}

pickFruits().then(console.log)

apple + banana๊ฐ€ ๋ฐ”๋กœ ์ถœ๋ ฅ๋œ๋‹ค.


์ง„ํ–‰ ๊ณผ์ •

pickFruits ์‹คํ–‰ ํ•˜๋ฉด์„œ ๊ฐ€์žฅ ๋จผ์ € getApple์„๋ณด๊ณ 
ํ•ด๋‹น ๋‚ด์šฉ์—์„œ delayํ•จ์ˆ˜๋ฅผ ๋ณด๋Š”๋ฐ ํ•ด๋‹น delayํ•จ์ˆ˜๊ฐ€
๋ฐ”๋กœ ๋ฆฌํ„ดํ•˜๋Š” resolve๋ƒ or setTimeout๊ฐ™์€ ํ•จ์ˆ˜๋‚ด์—์„œ ์ด๋”ฐ๊ฐ€ ์ง„ํ–‰ํ•˜๋Š” resolve๋ƒ๋ฅผ ์ฒดํฌํ•˜๋Š”๋ฐ ๋ฐ”๋กœ ๋ฆฌํ„ดํ•˜๋ฏ€๋กœ ๊ทธ ๋‹ค์Œ ๋ฌธ์žฅ์ธ apple๋กœ ๋ฆฌํ„ด์„ ๋ฐ”๋กœํ•˜๊ณ  getBananaํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ ํ›„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐ”๋กœ ๋ฆฌํ„ดํ•˜์—ฌ
a+b์ธ apple+banana๋ฅผ ๋ฐ”๋กœ ์ถœ๋ ฅํ•œ๋‹ค.


2. 10์ดˆ ๋’ค ์ง„ํ–‰


//10์ดˆ
function delay(ms){
  return new Promise(resolve =>setTimeout(resolve,ms));
}

async function getApple(){
  await delay(5000);      //delay์˜ resolve๊ฐ€ setTimeout์— ์˜ํ•ด ๊ธฐ๋‹ค๋ฆฌ๋‚˜ => ๊ธฐ๋‹ค๋ฆฐ๋‹ค. return ๋Šฆ๊ฒŒ
  return 'apple';
}
async function getBanana(){
  await delay(5000);      //delay์˜ resolve๊ฐ€ setTimeout์— ์˜ํ•ด ๊ธฐ๋‹ค๋ฆฌ๋‚˜ => ๊ธฐ๋‹ค๋ฆฐ๋‹ค. return ๋Šฆ๊ฒŒ
  return 'banana';
}

async function pickFruits(){
  const a = await getApple();     //await์— ์˜ํ•ด getApple์˜ return ์ž ๊น ๊ธฐ๋‹ค๋ ค => ๋Œ€๊ธฐ
  const b = await getBanana();    //5์ดˆ๋’ค ์ง„ํ–‰๋˜๋ฉด getBanana์˜ return ์ž ๊น ๊ธฐ๋‹ค๋ ค => ๋Œ€๊ธฐ
  return `${a} + ${b}`;
}

pickFruits().then(console.log)

apple + banana๊ฐ€ 10์ดˆ ๋’ค์— ์ถœ๋ ฅ๋œ๋‹ค.


์ง„ํ–‰๊ณผ์ •

pickFruits ์‹คํ–‰ ํ•˜๋ฉด์„œ ๊ฐ€์žฅ ๋จผ์ € getApple์„๋ณด๊ณ 
ํ•ด๋‹น ๋‚ด์šฉ์—์„œ delayํ•จ์ˆ˜๋ฅผ ๋ณด๋Š”๋ฐ ํ•ด๋‹น delayํ•จ์ˆ˜๊ฐ€
๋ฐ”๋กœ ๋ฆฌํ„ดํ•˜๋Š” resolve๋ƒ or setTimeout๊ฐ™์€ ํ•จ์ˆ˜๋‚ด์—์„œ ์ด๋”ฐ๊ฐ€ ์ง„ํ–‰ํ•˜๋Š” resolve๋ƒ๋ฅผ ์ฒดํฌํ•˜๋Š”๋ฐ setTimeout ์•ˆ์— resolve๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์ „๋ถ€ ๋Œ€๊ธฐํ•˜๋Š” ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ณ  5์ดˆ ๋’ค apple์„ ๋ฆฌํ„ดํ•˜๊ณ  a์— ๋„ฃ๊ณ  ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ
getBanana๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ setTimeout ์•ˆ์— resolve๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์ „๋ถ€ ๋Œ€๊ธฐํ•˜๋Š” ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ณ  5์ดˆ ๋’ค banana๋ฅผ ๋„ฃ์–ด a+b๋ฅผ ์ถœ๋ ฅํ•˜๋ฏ€๋กœ
์ถœ๋ ฅ๊นŒ์ง€ ์ด, 10์ดˆ๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค.


3. 5์ดˆ ๋’ค ์ง„ํ–‰


// 5์ดˆ

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

async function getApple(){
  await delay(5000);
  return 'apple';
}
async function getBanana(){
  await delay(5000);
  return 'banana';
}

async function pickFruits(){
  //promise๋ฅผ ๋งŒ๋“œ๋Š” ์ˆœ๊ฐ„ ๋ฐ”๋กœ ์‹คํ–‰์ด๋œ๋‹ค. (์„œ๋กœ์—๊ฒŒ ์—ฐ๊ด€์„ ์ฃผ์ง€์•Š๊ณ )
  const applePromise = getApple();
  const bananaPromise = getBanana();

  const a = await applePromise;
  const b = await bananaPromise;
  return `${a} + ${b}`;
}

pickFruits().then(console.log)

apple + banana๊ฐ€ 5์ดˆ ๋’ค์— ์ถœ๋ ฅ๋œ๋‹ค.


์ง„ํ–‰๊ณผ์ •

ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ await๋กœ ๋ฌถ์—ฌ์žˆ์ง€์•Š๊ณ  ์ •์˜๋˜์–ด ๊ฐ๊ฐ promise๋ฅผ ๋งŒ๋“œ๋Š” ์ˆœ๊ฐ„ ๋ฐ”๋กœ ์‹คํ–‰์ด ๋˜์–ด ๋ณ‘๋ ฌ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋œ ๊ฐ’์ด a,b๋กœ ๋„˜๊ฒจ์ ธ ์ถœ๋ ฅ๋˜๋ฏ€๋กœ
๊ฐ๊ฐ์˜ 5์ดˆ์”ฉ์„ ์ง„ํ–‰ํ•˜์—ฌ 5์ดˆ ๋’ค์— ์ถœ๋ ฅ๋œ๋‹ค.


4. 5์ดˆ ๋’ค ์ง„ํ–‰(promise.all)

//5์ดˆ Promise.all

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

async function getApple(){
  await delay(5000);
  return 'apple';
}
async function getBanana(){
  await delay(5000);
  return 'banana';
}

function pickAllFruits(){
  return Promise.all([getApple(),getBanana()])
    .then(fruits=>fruits.join(' + '))
}
pickAllFruits().then(console.log);

apple + banana๊ฐ€ 5์ดˆ ๋’ค์— ์ถœ๋ ฅ๋œ๋‹ค.


์ง„ํ–‰๊ณผ์ •

Promise.all์„ ์ด์šฉํ•ด ๊ฐ๊ฐ์„ ๋ณ‘๋ ฌ์ ์œผ๋กœ ์‹คํ–‰์‹œ์ผœ ์ฒ˜๋ฆฌํ•œ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜์—ฌ ๊ฐ๊ฐ์˜ 5์ดˆ์”ฉ์ด๋ฏ€๋กœ 5์ดˆ ๋’ค์— ์ถœ๋ ฅ๋œ๋‹ค.


์ •๋ฆฌ

async await๋Š” ์ผ๋‹จ ์ฒซ๋ฒˆ์งธ await์˜ ๋‚ด์šฉ์„ ํ™•์ธํ•˜์—ฌ ํ•ด๋‹น
resolve๊ฐ€ ๋ฐ”๋กœ ๋ฆฌํ„ด์ด๋ƒ / setTimeout ๊ฐ™์€ ๊ฒƒ์— ์˜ํ–ฅ์„ ๋ฐ›๋ƒ์— ๋”ฐ๋ผ
๋‹ค๋ฅธ ์•„๋ž˜์˜ await๊ฐ€ ๋ฐ”๋กœ ๋ฆฌํ„ด์ด๋ฉด ๊ธฐ๋‹ค๋ฆฌ์ง€์•Š๊ณ  ์ง„ํ–‰๋˜๊ณ (๋ณ‘๋ ฌ) setTimeout์— ์˜ํ–ฅ์„ ๋ฐ›์œผ๋ฉด ํ•ด๋‹น ์‹œ๊ฐ„๋งŒํผ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ๋‹ค์Œ await์„ ์‹คํ–‰ํ•œ๋‹ค.(์ง๋ ฌ)

profile
๋‚˜์˜ ํ•˜๋ฃจ๋ฅผ ๊ฐ€๋Šฅํ•œ ๊ธฐ์–ตํ•˜๊ณ  ์ฆ๊ธฐ๊ณ  ํ›„ํšŒํ•˜์ง€๋ง์ž

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