Promises and async patterns
Run independent work in parallel with `Promise.all`. Use `allSettled` when one failure shouldn't kill the batch, and `race`/`AbortController` for timeouts.
#promise#async#await#abort
javascript
// parallel — fails fast
const [user, posts] = await Promise.all([getUser(1), getPosts(1)]);
// parallel — collect successes and failures
const results = await Promise.allSettled([a(), b()]);
const ok = results.filter((r) => r.status === "fulfilled");
// timeout
const ctrl = new AbortController();
setTimeout(() => ctrl.abort(), 5000);
await fetch("/slow", { signal: ctrl.signal });Related
fetch JSON with error handling
`fetch` only rejects on network failure — a 404 or 500 still resolves. Always check `res.ok` yourself.
Array methods you reach for
`map` transforms, `filter` narrows, `reduce` folds. All three return new values — none of them mutate. `sort` and `reverse` DO mutate, so copy first with `toSorted` or a spread.