Async and Await
A promise represents a value that may be ready later. async and await let you work with promises using clean, readable code.
Marking a function async lets you await a promise inside it, pausing until the value is ready.
async function load() {
const value = await Promise.resolve("done");
return value;
}
load().then(result => console.log(result));
- await only works inside an async function
- An async function always returns a promise
- Use try/catch to handle errors from await
Here we use Promise.resolve so the example runs instantly with no network call.
Try it yourself
Loading editor…