await

The await operator is used to wait for a Promise. It can only be used inside an async function.

Syntax

[rv] = await expression;
expression
A Promise or any value to wait for.
rv

Returns the fulfilled value of the promise, or the value itself if it's not a Promise.

Description

The await expression causes async function execution to pause until a Promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfillment. When resumed, the value of the await expression is that of the fulfilled Promise.

If the Promise is rejected, the await expression throws the rejected value.

If the value of the expression following the await operator is not a Promise, it's converted to a resolved Promise.

An await splits execution flow, allowing the caller of the async function to resume execution. After the await defers the continuation of the async function, execution of subsequent statements ensues. If this await is the last expression executed by its function execution continues by returning to the function's caller a pending Promise for completion of the await's function and resuming execution of that caller.

Examples

Awaiting a promise to be fulfilled

If a Promise is passed to an await expression, it waits for the Promise to be fulfilled and returns the fulfilled value.

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

async function f1() {
  var x = await resolveAfter2Seconds(10);
  console.log(x); // 10
}

f1();

Thenable objects

Thenable objects will be fulfilled just the same.

async function f2() {
  const thenable = {
    then: function(resolve, _reject) {
      resolve('resolved!')
    }
  };
  console.log(await thenable); // resolved!
}

f2();

Conversion to promise

If the value is not a Promise, it converts the value to a resolved Promise, and waits for it.

async function f3() {
  var y = await 20;
  console.log(y); // 20
}

f3();

Promise rejection

If the Promise is rejected, the rejected value is thrown.

async function f4() {
  try {
    var z = await Promise.reject(30);
  } catch(e) {
    console.error(e); // 30
  }
}

f4();

Handling rejected promises

Handle rejected Promise without try block.

var response = await promisedFunction().catch((err) => { console.error(err); });
// response will be undefined if the promise is rejected

Specifications

Specification
ECMAScript (ECMA-262)
The definition of 'async functions' in that specification.

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
awaitChrome Full support 55Edge Full support 14Firefox Full support 52IE No support NoOpera Full support 42Safari Full support 10.1WebView Android Full support 55Chrome Android Full support 55Firefox Android Full support 52Opera Android Full support 42Safari iOS Full support 10.3Samsung Internet Android Full support 6.0nodejs Full support 7.6.0
Full support 7.6.0
Full support 7.0.0
Disabled
Disabled From version 7.0.0: this feature is behind the --harmony runtime flag.
Use at module top level
Experimental
Chrome No support No
Notes
No support No
Notes
Notes See bug 9344
Edge No support NoFirefox No support No
Notes
No support No
Notes
Notes See bug 1519100
IE No support NoOpera No support NoSafari No support No
Notes
No support No
Notes
Notes See bug 202484
WebView Android No support NoChrome Android No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs Full support 14.8.0
Notes
Full support 14.8.0
Notes
Notes Not supported in CommonJS modules.
Full support 14.3.0
Notes Disabled
Notes Not supported in CommonJS modules.
Disabled From version 14.3.0: this feature is behind the --experimental-top-level-await runtime flag.

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also