Promise Cheatsheet - Async JavaScript

This reference is for JavaScript developers writing async code, whether in the browser with fetch or in Node. It starts from creating and consuming a promise, then static combinators — all vs race vs allSettled vs any, which beginners often mix up — before chaining and the async/await sugar it sits on. Later sections cover real problems: running many requests concurrently without flooding, waiting for the fastest result with a timeout, retrying a flaky request, and aborting an in-flight fetch with AbortController. After reading you should be able to sequence async steps, run parallel tasks with a concurrency cap, and add timeout and retry to brittle calls.

Languages·36 commands·Last updated 2026-07-21
promiseAsyncasyncjavascript

Basics 5

new Promise((resolve, reject) => { resolve(value) })
Create and resolve immediately
new Promise((resolve, reject) => { reject(new Error("fail")) })
Create and reject immediately
Promise.resolve(value)
Create a resolved Promise directly
Promise.reject(reason)
Create a rejected Promise directly
new Promise((resolve) => setTimeout(resolve, 1000, "ok"))
Resolve after a 1s delay

Consuming Promises 5

promise.then((result) => {})
Register a success callback
promise.catch((error) => {})
Register a failure callback
promise.finally(() => {})
Run regardless of outcome
promise.then(onFulfilled, onRejected)
Specify both success and failure callbacks
promise.then((v) => v + 1)
then return value is auto-wrapped into a new Promise

Static Methods 5

Promise.all([p1, p2])
Succeeds only if all succeed; fails if any fails
Promise.race([p1, p2])
Result of the first to settle (success or fail)
Promise.allSettled([p1, p2])
Wait for all to settle, return each result's status
Promise.any([p1, p2])
First to succeed; fails only if all fail
Promise.allSettled result item = { status, value } or { status, reason }
Result object shape

Chaining 4

fetch(url).then((r) => r.json()).then((data) => {})
Chain to process the response
promise.then(() => nextPromise())
Return a new Promise to sequence async work
promise.then((v) => v + 1).then((v) => console.log(v))
Values pass downstream
promise.catch((err) => fallback()).then((v) => {})
Recover from error then continue the chain

async/await 5

async function fn() { return value }
async function auto-returns a Promise
const result = await promise
Wait for the Promise to settle
try { await promise } catch (err) {}
Catch rejections with try/catch
await Promise.all([p1, p2])
Await multiple tasks concurrently
const [a, b] = await Promise.all([fa(), fb()])
Await concurrently and destructure results

Advanced Patterns 6

for (const item of items) { await process(item) }
Run serially (await one by one)
await Promise.all(items.map((i) => fetch(i)))
Run all tasks concurrently
async function pool(tasks, limit) {}
Limit max concurrency
Promise.race([task(), timeout(5000)])
Timeout control (5s)
async function retry(fn, times = 3) {}
Auto-retry on failure
async function sleep(ms) { return new Promise((r) => setTimeout(r, ms)) }
Delay utility function

Cancellation & Utilities 6

const ac = new AbortController(); ac.abort()
Create and trigger an abort signal
fetch(url, { signal: ac.signal })
Associate the abort signal with fetch
ac.signal.addEventListener("abort", () => {})
Listen for the abort event
AbortSignal.timeout(3000)
Auto-abort after 3s
queueMicrotask(() => {})
Push a callback into the microtask queue
const d = {}; d.promise = new Promise((r, j) => { d.resolve = r; d.reject = j })
Deferred pattern (control resolve externally)

Tips

  • A Promise has three states: pending, fulfilled, rejected - irreversible once settled.
  • then returns a new Promise; chained return values are auto-wrapped and passed to the next then.
  • catch equals then(null, onRejected) and catches rejections anywhere in the chain.
  • Use all when all must succeed, allSettled when you need every result, race for the fastest, any for the first success.
  • async/await is syntactic sugar over Promises; try/catch makes error handling clearer.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us