Skip to content

파트11. 비동기 동시성 프로그래밍(3)

Yongku cho edited this page May 22, 2019 · 4 revisions

await는 Promise.resolve 값을 좌항에 바인딩합니다. async 함수 실행 결과는 Promise가 전달됩니다.

const delay = (time, value) => new Promise(resolve => {
  setTimeout(() => resolve(value), time);
});

async function f() {
  const a = await delay(1000, 'a');
  const b = await delay(2000, 'b');
  return `${a}${b}`;
}

f().then(console.log) // ab

실험해본것

await에 reject이되면 async의 catch로 가게됩니다.

const delay = (time, value) => new Promise(resolve => {
  setTimeout(() => resolve(value), time);
});

async function f() {
  const a = await delay(1000, 'a');
  const b = await Promise.reject('에러 발생!');
  return `${a}${b}`;
}

f().catch(console.log) // 에러 발생!
Clone this wiki locally