➕await
turns Promise (future value) into a value/error.
JS ⟩ statement ⟩ expression ⟩ operator ⟩ unary ⟩ await
(⭐️ ES2017) (primary expression) (unary operator) turns a Promise / thenable into a return value / thrown exception.
if resolves normally, await returns the result.
if rejected, it throws error (as if there were a
throw
statement at that line).
// works only inside async functions❗️
let value = await promise;
await can only be used inside an async function❗
modern browsers allow top-level await in modules.
// when top-level await not supported, use async IIFE instead.
(async () => {
let value = await promise;
// ...
})();
Last updated
Was this helpful?