Наші добровольці ще не переклали статтю цією мовою: Українська. Долучайтеся й допоможіть це зробити!
Ви також можете прочитати цю статтю іншою мовою: English (US).

The async function declaration defines an asynchronous function, which returns an AsyncFunction object.

You can also define async functions using an async function expression.


async function name([param[, param[, ... param]]]) {


The function name.
The name of an argument to be passed to the function.
The statements comprising the body of the function.

Return value

A Promise which will be resolved with the value returned by the async function, or rejected with an uncaught exception thrown from within the async function.


An async function can contain an await expression that pauses the execution of the async function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value.

Remember, the await keyword is only valid inside async functions. If you use it outside of an async function's body, you will get a SyntaxError.

The purpose of async/await functions is to simplify the behavior of using promises synchronously and to perform some behavior on a group of Promises. Just as Promises are similar to structured callbacks, async/await is similar to combining generators and promises.


Simple example

function resolveAfter2Seconds(x) {
  return new Promise(resolve => {
    setTimeout(() => {
    }, 2000);

async function add1(x) {
  const a = await resolveAfter2Seconds(20);
  const b = await resolveAfter2Seconds(30);
  return x + a + b;

add1(10).then(v => {
  console.log(v);  // prints 60 after 4 seconds.

async function add2(x) {
  const p_a = resolveAfter2Seconds(20);
  const p_b = resolveAfter2Seconds(30);
  return x + await p_a + await p_b;

add2(10).then(v => {
  console.log(v);  // prints 60 after 2 seconds.

Do not confuse await for Promise.all

In add1, execution suspends 2 seconds for the first await, and then again another 2 seconds for the second await. The second timer is not created until the first has already fired. In add2,  both timers are created, and then both are awaited. This leads it to resolve in 2 rather than 4 seconds, because the timers are running concurrently. But both of the await calls are still run in series, not in parallel: this is not some automatic application of Promise.all. If you wish to await two or more promises in parallel, you must still use Promise.all.

Rewriting a promise chain with an async function

An API that returns a Promise will result in a promise chain, and it splits the function into many parts. Consider the following code:

function getProcessedData(url) {
  return downloadData(url) // returns a promise
    .catch(e => {
      return downloadFallbackData(url)  // returns a promise
    .then(v => {
      return processDataInWorker(v); // returns a promise

it can be rewritten with a single async function as follows:

async function getProcessedData(url) {
  let v;
  try {
    v = await downloadData(url); 
  } catch(e) {
    v = await downloadFallbackData(url);
  return processDataInWorker(v);

Note that in the above example, there is no await statement on the return statement, because the return value of an async function is implicitly wrapped in Promise.resolve.


Specification Status Comment
ECMAScript Latest Draft (ECMA-262)
The definition of 'async function' in that specification.
Draft Initial definition in ES2017.
ECMAScript 2017 (ECMA-262)
The definition of 'async function' in that specification.

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support55 Yes52 No4210.1
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes55 Yes524210.1 ?

See also

Мітки документа й учасники

 Востаннє оновлена: doubleOrt,