await
Pauses an async function until a promise settles and yields its resolved value.
Syntax
const value = await promise;
Returns
any — The fulfillment value of the awaited promise; throws if it rejects.
Throws
any— The awaited promise rejects, propagating the rejection reason.
Examples
function delay(ms, v) {
return new Promise(r => setTimeout(() => r(v), ms));
}
async function main() {
const x = await delay(0, 7);
console.log(x);
}
main();
Output
7
async function run() {
try {
await Promise.reject(new Error("nope"));
} catch (e) {
console.log(e.message);
}
}
run();
Output
nope
async function values() {
const [a, b] = await Promise.all([
Promise.resolve(1),
Promise.resolve(2),
]);
console.log(a + b);
}
values();
Output
3
Notes
- `await` is valid inside async functions and at module top level.
- Awaiting a non-promise wraps it and resolves immediately.
- Wrap awaited calls in `try...catch` to handle rejections.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 55 |
| firefox | 52 |
| safari | 11 |
| edge | 15 |
| node | 7.6 |