Example 1 — await inside
Pause for a promise.
JavaScript
async function run() {
const v = await Promise.resolve(7);
console.log(v);
}
run();How It Works
- await waits for fulfillment.
- Then logging continues.
An async function always returns a Promise. Inside it you can await other promises. Thrown errors become rejected promises.
async function load() {
return "ready";
}
load().then((v) => console.log(v));Pause for a promise.
async function run() {
const v = await Promise.resolve(7);
console.log(v);
}
run();try/catch works with await.
async function boom() {
throw new Error("fail");
}
bloom().catch((e) => console.log(e.message));const form.
const f = async () => 1;
f().then(console.log);CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful