The await
operator is used to wait for a Promise
. It can only be used inside an async function or a JavaScript module.
The await
operator is used to wait for a Promise
. It can only be used inside an async function or a JavaScript module.
[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
.
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.
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() { const x = await resolveAfter2Seconds(10); console.log(x); // 10 } f1();
Thenable objects will be fulfilled just the same.
async function f2() { const thenable = { then(resolve, _reject) { resolve('resolved!') } }; console.log(await thenable); // resolved! } f2();
If the value is not a Promise
, it converts the value to a resolved Promise
, and waits for it.
async function f3() { const y = await 20; console.log(y); // 20 } f3();
If the Promise
is rejected, the rejected value is thrown.
async function f4() { try { const z = await Promise.reject(30); } catch (e) { console.error(e); // 30 } } f4();
Handle rejected Promise
without try block.
const response = await promisedFunction() .catch((err) => { console.error(err); }); // response will be undefined if the promise is rejected
You can use the await
keyword on its own (outside of an async function) within a JavaScript module. This means modules, with child modules that use await
, wait for the child module to execute before they themselves run. All while not blocking other child modules from loading.
Here is an example of a simple module using the Fetch API and specifying await within the export statement
. Any modules that include this will wait for the fetch to resolve before running any code.
// fetch request const colors = fetch('../data/colors.json') .then((response) => response.json()); export default await colors;
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
await |
55 |
14 |
52 |
No |
42 |
10.1 |
55 |
55 |
52 |
42 |
10.3 |
6.0 |
1.0 |
7.6.0
7.0.0
|
top_level |
89 |
89 |
89 |
No |
75
73-75
|
15 |
89 |
89 |
89 |
63 |
15 |
15.0 |
1.0 |
14.8.0
Not supported in CommonJS modules.
14.3.0
Not supported in CommonJS modules.
|
async function
async function
expressionAsyncFunction
object
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await