Async await - Lee-hyuna/33-js-concepts-kr GitHub Wiki

Async/await

๋ฒˆ์—ญ : https://javascript.info/async-await

์—ฌ๊ธฐ promises์™€ ํ•จ๊ป˜ ์ž‘๋™๋˜๋Š” "async/await" ์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๋ฌธ๋ฒ•์ด ์žˆ๋‹ค. ๋งค์šฐ ๋†€๋ž๋„๋ก ์ดํ•ดํ•˜๊ธฐ์™€ ์‚ฌ์šฉํ•˜๊ธฐ๊ฐ€ ์‰ฝ๋‹ค.

Async functions

async ํ‚ค์›Œ๋“œ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž. ์ด ํ‚ค์›Œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•จ์ˆ˜ ์•ž๋ถ€๋ถ„์— ์œ„์น˜ํ•œ๋‹ค.

async function f() {
  return 1;
}

ํ•จ์ˆ˜ ์•ž์˜ async ๋‹จ์–ด๋Š” ํ•œ๊ฐ€์ง€ ๋œป์„ ์ง€๋‹Œ๋‹ค: ์ด ํ•จ์ˆ˜๋Š” ํ•ญ์ƒ promise๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๊ฐ’๋“ค์€ ์ž๋™์ ์œผ๋กœ resolve๋œ promise๋กœ ๊ฐ์‹ธ์ง‘๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์ด ํ•จ์ˆ˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ 1์ธ resolve๋œ promise๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

async function f() {
  return 1;
}

f().then(alert); // 1

์šฐ๋ฆฌ๋Š” ์•”๋ฌต์ ์œผ๋กœ promise๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

async function f() {
  return Promise.resolve(1);
}

f().then(alert); // 1

๊ทธ๋ž˜์„œ async ๋Š” ํ•จ์ˆ˜๊ฐ€ promise๋ฅผ ๋ฆฌํ„ด ํ•˜๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•˜๊ณ , promise๊ฐ€ ์•„๋‹Œ ๊ฐ’์„ ํฌ์žฅํ•ฉ๋‹ˆ๋‹ค. ๋งค์šฐ ๊ฐ„๋‹จํ•˜์ง€ ์•Š์Šต๋‹ˆ๊นŒ? ์ด๊ฒŒ ๋์ด ์•„๋‹™๋‹ˆ๋‹ค. await ์€ ์˜ค์ง async ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋งค์šฐ ์ฟจํ•ฉ๋‹ˆ๋‹ค.

Await

๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

// works only inside async functions
let value = await promise;

await ํ‚ค์›Œ๋“œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ promise๊ฐ€ ์…‹ํŒ…๋˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ ๊ฐ’์ด ๋ฆฌํ„ด๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ 1์ดˆ ํ›„์— resolve๊ฐ€ ๋˜๋Š” promise ์˜ˆ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

async function f() {

  let promise = new Promise((resolve, reject) => {
    setTimeout(() => resolve("done!"), 1000)
  });

  let result = await promise; // wait till the promise resolves (*)

  alert(result); // "done!"
}

f();

์ด ํ•จ์ˆ˜ ์‹คํ–‰์€ (*) ํ‘œ์‹œ ํ•ด๋‘” ๊ณณ์—์„œ ์ž ์‹œ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณค promise๊ฐ€ ํ•ด๊ฒฐ ๋˜์—ˆ์„๋•Œ ๋‹ค์‹œ ์žฌ๊ฐœ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์œ„ ์ฝ”๋“œ์—์„œ 1์ดˆ ๋’ค์— "done!" ์ด๋ผ๋Š” ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๊ฐ•์กฐํ•ฉ์‹œ๋‹ค: await ์€ ๋ฌธ์ž ๊ทธ๋Œ€๋กœ promise๊ฐ€ ํ•ด๊ฒฐ๋  ๋•Œ๊นŒ์ง€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋ฉด ๊ณ„์† ์ง„ํ–‰์ด ๋ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ CPU ๋น„์šฉ(๋‚ญ๋น„)์ด ๋“ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์—”์ง„์€ ๊ทธ ๋™์•ˆ์— ๋‹ค๋ฅธ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. : ๋‹ค๋ฅธ ์Šคํฌ๋ฆฝํŠธ๋‚˜ ๋‹ค๋ฅธ ์ด๋ฒคํŠธ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๊ฒƒ์€ promise๋ฅผ ์–ป๊ธฐ์— promise.then ๋ณด๋‹ค ๋งค์šฐ ์šฐ์•„ํ•œ ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ฝ๊ณ  ์“ฐ๊ณ ๊ฐ€ ์‰ฝ์Šต๋‹ˆ๋‹ค.


Canโ€™t use await in regular functions

๋งŒ์•ฝ await ์„ ๋™๊ธฐ์‹ ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋ฌธ๋ฒ• ์—๋Ÿฌ๊ฐ€ ๋‚ ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

function f() {
  let promise = Promise.resolve(1);
  let result = await promise; // Syntax error
}

ํ•จ์ˆ˜ ์•ž์— async๋ฅผ ๋ถ™์ด์ง€ ์•Š์•˜๋‹ค๋ฉด ์—๋Ÿฌ๋ฅผ ๋งˆ์ฃผํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ await ์€ ์˜ค์ง async ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ž‘๋™ ๋ฉ๋‹ˆ๋‹ค.


Promises chaining ์ฑ•ํ„ฐ์—์„œ ์ผ๋˜ showAvarar()๋ฅผ ๊ฐ€์ ธ์˜ต์‹œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  async/await ์„ ์‚ฌ์šฉํ•ด์„œ ๋‹ค์‹œ ์ž‘์„ฑํ•ด ๋ด…์‹œ๋‹ค.

  1. ์šฐ๋ฆฌ๋Š” .then ํ˜ธ์ถœ์„ await์œผ๋กœ ๋ฐ”๊ฟ€ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ๋˜ํ•œ ์šฐ๋ฆฌ๋Š” ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด async ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
async function showAvatar() {

  // read our JSON
  let response = await fetch('/article/promise-chaining/user.json');
  let user = await response.json();

  // read github user
  let githubResponse = await fetch(`https://api.github.com/users/${user.name}`);
  let githubUser = await githubResponse.json();

  // show the avatar
  let img = document.createElement('img');
  img.src = githubUser.avatar_url;
  img.className = "promise-avatar-example";
  document.body.append(img);

  // wait 3 seconds
  await new Promise((resolve, reject) => setTimeout(resolve, 3000));

  img.remove();

  return githubUser;
}

showAvatar();

๋งค์šฐ ์ฝ๊ธฐ ์‰ฌ์›Œ์ง€์ง€ ์•Š์•˜์Šต๋‹ˆ๊นŒ? ์ด์ „๋ณด๋‹ค ํ›จ์”ฌ ๋‚˜์•„์กŒ์Šต๋‹ˆ๋‹ค.


await wonโ€™t work in the top-level code

await์„ ์ฒ˜์Œ ์“ฐ๋Š” ์‚ฌ๋žŒ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‚ฌ์‹ค์„ ์žŠ์–ด๋ฒ„๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ฝ”๋“œ์˜ ์ตœ์ƒ๋‹จ์— await์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋Š” ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

// syntax error in top-level code
let response = await fetch('/article/promise-chaining/user.json');
let user = await response.json();

์šฐ๋ฆฌ๋Š” ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋กœ ๊ฐ์‹ธ์„œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

(async () => {
  let response = await fetch('/article/promise-chaining/user.json');
  let user = await response.json();
  ...
})();

await accepts โ€œthenablesโ€

promise.then ์ฒ˜๋Ÿผ, await๋Š” then ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์•„์ด๋””์–ด๋Š” ์„œ๋“œํŒŒํ‹ฐ ๊ฐ์ฒด๊ฐ€ ๋งŒ์•ฝ์— promise๊ฐ€ ์•„๋‹ˆ์ง€๋งŒ promise ์ฒ˜๋Ÿผ ์›€์ง์ผ์ˆ˜ ์žˆ๋Š”: ๋งŒ์•ฝ .then ์„ ์ง€์›ํ•˜๋Š” ๊ฐ์ฒด๋ผ๋ฉด ์ถฉ๋ถ„ํžˆ await ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ ๋ฐ๋ชจ๋Š” Thenable ํด๋ž˜์Šค์™€ await์ด ์ด ์ธ์Šคํ„ด์Šค์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

class Thenable {
  constructor(num) {
    this.num = num;
  }
  then(resolve, reject) {
    alert(resolve);
    // resolve with this.num*2 after 1000ms
    setTimeout(() => resolve(this.num * 2), 1000); // (*)
  }
};

async function f() {
  // waits for 1 second, then result becomes 2
  let result = await new Thenable(1);
  alert(result);
}

๋งŒ์•ฝ์— await์ด .then ์„ ๊ฐ–๋Š” promise ๊ฐ์ฒด๊ฐ€ ์•„๋‹๊ฒฝ์šฐ ๊ทธ๊ฒƒ์€ ๋„ค์ดํ‹ฐ๋ธŒ ํ•จ์ˆ˜ resolve, reject ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ œ๊ณต๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  await์€ ๊ทธ์ค‘ ํ•˜๋‚˜๊ฐ€ ํ˜ธ์ถœ ๋ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค.


Async class method

๋น„๋™๊ธฐ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹จ์ง€ async๋งŒ ํ•จ์ˆ˜ ์•ž์— ๋ถ™์—ฌ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

class Waiter {
  async wait() {
    return await Promise.resolve(1);
  }
}

new Waiter()
  .wait()
  .then(alert); // 1

์ด๊ฒƒ์˜ ์˜๋ฏธ๋Š” ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฐ˜ํ™˜๋ฐ›๋Š” ๊ฐ’์ด promise์ด๊ณ  await ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ณด์žฅ์„ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Error handling

๋งŒ์•ฝ promise ๊ฐ€ ํ‰๋ฒ”ํ•˜๊ฒŒ ๊ท€๊ฒฐ์ด ๋˜์—ˆ๋‹ค๋ฉด, await promise ๋Š” ๊ฐ’์„ ๋ฆฌํ„ด ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฑฐ์ ˆ์˜ ์ผ€์ด์Šค์—์„œ๋Š” error๋ฅผ ๋˜์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•ด๋‹น ๋ผ์ธ์— throw ๋ฌธ์„ ๋งŒ๋‚˜๊ฒŒ ๋œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ

async function f() {
  await Promise.reject(new Error("Whoops!"));
}

์ด๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

async function f() {
  throw new Error("Whoops!");
}

์‹ค์ œ ์ƒํ™ฉ์—์„œ promise๋Š” ์•„๋งˆ reject ํ•˜๊ธฐ์ „์— ๋ช‡๋ถ„์˜ ์‹œ๊ฐ„์„ ๋ณด๋‚ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ await ์ด ์—๋Ÿฌ๋ฅผ ๋˜์ง€๊ธฐ ๊นŒ์ง€ delay ๊ฐ€ ์žˆ์„ ๊ฑด๋ฐ์š”.

์šฐ๋ฆฌ๋Š” ์ด๋•Œ ์—๋Ÿฌ๋ฅผ try...catch ๋ฌธ์œผ๋กœ ์žก์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

async function f() {

  try {
    let response = await fetch('http://no-such-url');
  } catch(err) {
    alert(err); // TypeError: failed to fetch
  }
}

f();

์—๋Ÿฌ์˜ ๊ฒฝ์šฐ catch ๋ธ”๋Ÿญ์œผ๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋˜ํ•œ ์—ฌ๋Ÿฌ์ค„์„ ๊ฐ์‹ธ ์ค„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

async function f() {

  try {
    let response = await fetch('/no-user-here');
    let user = await response.json();
  } catch(err) {
    // catches errors both in fetch and response.json
    alert(err);
  }
}

f();

๋งŒ์•ฝ try...catch๋ฌธ์„ ๊ฐ€์ง€์ง€ ์•Š๋Š”๋‹ค๋ฉด f()๋ฅผ ํ˜ธ์ถœํ•ด ์ƒ์„ฑํ•œ promise๋Š” reject ๋ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์šฐ๋ฆฌ๋Š” .catch์—์„œ ๊ทธ๊ฒƒ์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

async function f() {
  let response = await fetch('http://no-such-url');
}

// f() becomes a rejected promise
f().catch(alert); // TypeError: failed to fetch // (*)

๋งŒ์•ฝ .catch ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ์žŠ์–ด๋ฒ„๋ ธ๋‹ค๋ฉด ์ด promise์˜ ์—๋Ÿฌ๋ฅผ ํ•ธ๋“ค๋ง ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” Error handling with promises ์ด ์ฑ•ํ„ฐ์—์„œ ์„ค๋ช…ํ•œ ์ „์—ญ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ช‡๋ช‡ ์—๋Ÿฌ๋ฅผ ์žก์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


async/await and promise.then/catch

์šฐ๋ฆฌ๊ฐ€ async/await์„ ์‚ฌ์šฉํ•  ๋•Œ, .then์ด ๊ฑฐ์˜ ํ•„์š”๊ฐ€ ์—†๋‹ค, ์™œ๋ƒํ•˜๋ฉด await ๋Š” ์šฐ๋ฆฌ๋ฅผ ์œ„ํ•ด ๊ธฐ๋‹ค๋ ค์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฌ๋Š” ๋Œ€๊ฒŒ .catch ๋Œ€์‹ ์— try..catch๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ํ•ญ์ƒ ๊ทธ๋ ‡์ง„ ์•Š์„ ์ˆ˜ ์žˆ์ง€๋งŒ ๋Œ€๊ฒŒ ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ฝ”๋“œ ์ตœ์ƒ์œ„์—์„œ async ํ•จ์ˆ˜ ๋ฐ–์—์„œ ์‚ฌ์šฉํ• ๋•Œ ์šฐ๋ฆฌ๋Š” ๋ฌธ๋ฒ•์ ์œผ๋กœ await์„ ์‚ฌ์šฉํ• ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” ์ตœ์ข… ๊ฒฐ๊ณผ ๋˜๋Š” ์—๋Ÿฌ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด .then/catch ๋ฌธ์„ ์—ฐ์Šตํ•ด๋‘์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์œ„ ์˜ˆ์ œ์˜ (*) ๋ฌธ์—์„œ ๋ณด๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด์š”.


async/await works well with Promise.all

์šฐ๋ฆฌ๊ฐ€ ์—ฌ๋Ÿฌ promise๋ฅผ ๊ธฐ๋‹ค๋ ค์•ผ ํ• ๋•Œ, ์šฐ๋ฆฌ๋Š” ์ด๊ฒƒ๋“ค์„ Promise.all ๊ณผ await ๋กœ ๊ฐ์Œ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// wait for the array of results
let results = await Promise.all([
  fetch(url1),
  fetch(url2),
  ...
]);

์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ๋ฌ์„ ๊ฒฝ์šฐ ์‹คํŒจ๊ฐ€ Promise.all ๋กœ ์ „ํŒŒ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฐœ์ƒ๋˜๋Š” ์˜ˆ์™ธ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ถ€๋ถ„์„ try...catch ๋กœ ๊ฐ์‹ธ ์žก์•„๋ƒ…๋‹ˆ๋‹ค.

Summary

ํ•จ์ˆ˜ ์ „์— async ํ‚ค์›Œ๋“œ๋ฅผ ์ ๋Š”๊ฒƒ์€ ๋‹ค์Œ ๋‘๊ฐ€์ง€ ํšจ๊ณผ๋ฅผ ๊ฐ–๋Š”๋‹ค.

  1. ํ•ญ์ƒ promise๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  2. ์•ˆ์ชฝ์— await ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

promise ์ „์— await ํ‚ค์›Œ๋“œ๋ฅผ ๋‹ค๋Š” ๊ฒƒ์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ promise ๊ฐ€ ํ•ด๊ฒฐ๋ ๋•Œ ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๊ฒŒ ๋งŒ๋“ ๋‹ค. ๊ทธ ํ›„์—

  1. ๋งŒ์•ฝ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ด ์˜ˆ์™ธ๊ฐ€ ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ๋‹ค. ๋งˆ์น˜ throw error ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด
  2. ๋ฐ˜๋ฉด์— ๊ฐ’์ด ๋ฆฌํ„ด ๋  ์ˆ˜ ์žˆ๋‹ค.

์ด๋“ค์€ ํ•จ๊ป˜ ์ฝ๊ณ  ์“ธ ์ˆ˜์žˆ๋Š” ๋น„๋™๊ธฐ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ์œ„ํ•œ ํ›Œ๋ฅญํ•œ ํ”„๋ ˆ์ž„ ์›Œํฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

async / await๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด promise.then / catch ๋ฅผ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ๊ฑฐ์˜ ์—†์ง€๋งŒ promise์„ ๊ธฐ๋ฐ˜์œผ๋กœํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋ง์•„์•ผํ•ฉ๋‹ˆ๋‹ค. ๋•Œ๋กœ๋Š” (์˜ˆ : ๊ฐ€์žฅ ๋ฐ”๊นฅ ์ชฝ ๋ฒ”์œ„์—์„œ) ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ Promise.all ์€ ๋งŽ์€ ์ž‘์—…์„ ๋™์‹œ์— ๊ธฐ๋‹ค๋ฆด ์ˆ˜์žˆ๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.