03. Await Expression

Если в асинхронной функции встречается выражение с ключевым словом await, значением которого является Promise, то она приостанавливает свою работу (как генератор) до тех пор, пока Promise не будет установлен:

  • If the Promise is fulfilled, the result of await is the fulfillment value.

  • If the Promise is rejected, await throws the rejection value.

Every await expression suspend execution. A function with multiple await expressions in it will be suspended once at a time on each await expression until that Promise is settled, before unsuspending execution and moving onto the next await expression

Handling a single asynchronous result

async function asyncFunc() {
  const result = await otherAsyncFunc();
  console.log(result);
}

// Equivalent to:
function asyncFunc() {
  return otherAsyncFunc().then(result => {
    console.log(result);
  });
}

Detach promise from await

Можно отделять вызов async операции от await результата на ней:

Handling multiple asynchronous results in parallel

Note that await is sequential, Promise.all() is parallel:

Last updated

Was this helpful?