← Cheat Sheets
JavaScript Promises and Async/Await
Handle asynchronous work.
# JavaScript Promises and Async/Await
Handle asynchronous work.
```js
const wait = ms => new Promise(res => setTimeout(res, ms));
async function load() {
try {
const res = await fetch("/api/items");
if (!res.ok) throw new Error("HTTP " + res.status);
return await res.json();
} catch (err) {
console.error(err);
} finally {
console.log("done");
}
}
fetch(url).then(r => r.json()).catch(console.error);
await Promise.all([a(), b()]); // all succeed, or the first rejection
await Promise.allSettled([a(), b()]); // wait for every result
await Promise.race([a(), b()]); // first to settle
await Promise.any([a(), b()]); // first to succeed
```