Our volunteers haven't translated this article into বাংলা (বাংলাদেশ) yet. Join us and help get the job done!
You can also read the article in English (US).

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 resolved, 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.

Examples

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();

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

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

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

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

Handle rejected Promise without try block.

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

Specifications

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

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support 55Edge Full support YesFirefox Full support 52IE ? Opera Full support 42Safari Full support 10.1WebView Android Full support 55Chrome Android Full support 55Edge Mobile Full support YesFirefox Android Full support 52Opera Android Full support 42Safari iOS Full support 10.1Samsung 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.

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
User must explicitly enable this feature.
User must explicitly enable this feature.

See also

ডকুমেন্ট ট্যাগ এবং অবদানকারী

সর্বশেষ হালনাগাদ করেছেন: ExE-Boss,