moves / javascript / promises

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

WWADD — Moves. Stacks. Judgment calls.

FREE REFERENCE · NO LOGIN · NO TRACKING