JavaScript 2025 cheatsheet

JavaScript 2025: Promises and fetch

8 common JavaScript 2025 tasks.

Fetch JSON

Request JSON and check for HTTP errors. fetch only rejects on network failures.

const res = await fetch("/api/users");
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const users = await res.json();

POST JSON

Send a JSON body with the right header.

const res = await fetch("/api/users", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "Ada" }),
});

Time out a request

Abort a request that takes longer than five seconds.

const res = await fetch(url, { signal: AbortSignal.timeout(5000) });

Handle errors in async code

Catch rejected promises and thrown errors with try and catch.

try {
  const data = await load();
} catch (error) {
  console.error("Failed:", error);
}

Run promises in parallel

Wait for all promises. It rejects as soon as one fails.

const [users, posts] = await Promise.all([getUsers(), getPosts()]);

Wait for all promises, whatever happens

Get the outcome of every promise, including failures.

const results = await Promise.allSettled([a(), b(), c()]);

Pause for a while

A promise that resolves after a delay.

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
await sleep(1000);

Debounce a function

Run a function only after calls stop for a moment, for example on search input.

function debounce(fn, delay) {
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => fn(...args), delay);
  };
}

Was this page helpful?