Builder15 min readBadge: Promise Keeper

Async JavaScript: promises, async and await

Why some code has to wait, what a promise is, how async and await read, how to catch errors and run things side by side, and the async bugs AI-written code makes most.

Why some code has to wait

A page's JavaScript does one thing at a time. Loading data from an API, asking a database or waiting for a timer can take a second or more, and if the code simply stopped and waited, the whole page would freeze: no scrolling, no clicks.

So slow work happens in the background, and your code gets a promise straight away: an object standing in for a value that isn't ready yet. A promise starts pending, then is either fulfilled with a value (the data arrived) or rejected with an error (the network failed).

async and await

await waits for a promise and hands you its value: const response = await fetch("/api/notes"). Only the function it's in pauses; the rest of the page keeps working. You can use await inside a function marked async, and at the top level of a module.

An async function always returns a promise, so whoever calls it needs await too: const notes = await getNotes(). Forget it and you get the promise itself, which shows up as Promise { <pending> } or [object Promise] where your data should be.

Older code chains .then() instead: fetch(url).then((response) => response.json()).then((data) => show(data)). It's the same promises underneath, and AI tools write both styles.

When things go wrong

  • A rejected promise throws an error at the await, so wrap the awaits in try { ... } catch (error) { ... } and show the user a friendly message.
  • fetch only rejects when the request can't happen at all, such as when you're offline. A 404 or a 500 still arrives as a response, with response.ok set to false, so check it before you read the body.
  • finally runs whether it worked or not: the place to hide a loading spinner.
  • An error nobody catches becomes an 'Uncaught (in promise)' error in the console, and often a blank or frozen screen for the user.

One after another, or side by side

Two awaits in a row run one after the other. That's right when the second needs the first's result, like loading a user and then their orders. When the jobs don't depend on each other, start them together and wait once: const [user, settings] = await Promise.all([getUser(), getSettings()]). Two one-second calls then take about one second, not two.

  • Promise.all fails as soon as any promise fails. Promise.allSettled waits for every one and tells you which worked.
  • for (const id of ids) { await save(id) } handles one item at a time, in order.
  • ids.forEach(async (id) => { await save(id) }) does not wait at all: forEach starts every call and moves straight on. Use for...of, or await Promise.all(ids.map(save)).
  • Hundreds of requests at once can hit rate limits. For big lists, work in small batches.

Async bugs to spot in AI-written code

  • A missing await: data shows up as [object Promise], or .json is not a function.
  • forEach with an async callback, followed by code that expects the work to be done.
  • No response.ok check, so an error page gets parsed as JSON ('Unexpected token <').
  • No try/catch around awaits that can fail, so one network blip breaks the page.
  • In React, useEffect's function can't be async itself: define an async function inside the effect and call it. In Next.js server components, you can await data directly.
  • Independent awaits written one after another, making a page slower than it needs to be.

Key terms

Async / await
Code for slow tasks like loading data: await waits for the answer.
Promise
An object standing in for a value that isn't ready yet. It's pending, then fulfilled with a value or rejected with an error.
Promise.all
Starts several promises side by side and waits for all of them; it fails as soon as any one fails.
Callback
A function you hand to other code to call later, like the one you pass to addEventListener or setTimeout.
fetch
The browser's built-in way to request a URL. It returns a promise of a response; check response.ok before using it.
API
An agreed way for one piece of software to ask another for something.
JSON
A simple text format for data that apps send to each other: names and values in curly braces.
Error message
The computer's description of what went wrong. Copy it exactly when asking for help.
See the full glossary

Try it

Watch code wait

  1. Open the 'Wait with async and await' starter in the code playground and run it. Remove one await: what changes?
  2. Open 'Side by side with Promise.all'. Compare the two times it prints, then add the failing call and catch the error.
  3. Open 'The forEach that doesn't wait' and fix it two ways: with for...of, and with Promise.all and map.
  4. Find an await in code your AI wrote. What happens if that promise rejects? Is there a try/catch, and is response.ok checked?
  5. Ask your AI helper: 'Which of these awaits could run side by side?' Check its answer: do the calls really not depend on each other?

Ask an AI tutor about this guide

Copy a prompt into ChatGPT, Claude or Gemini to go deeper. Check what it tells you against the guide.

Study with an AI tutor: copy this whole guide with tutoring instructions, then paste it into your AI assistant.
  • Explain it like I'm 12
  • Quiz me (5 questions, one at a time)
  • Show me a real example for my situation
  • What do beginners get wrong?

Knowledge check

Five questions. Get 4 right to earn the Promise Keeper badge. You can retake it as often as you like.

Question 1 of 5

const data = getNotes() prints Promise { <pending> } instead of the notes. Why?

Finished the How software works guides? Take the How software works track test for ten mixed questions.

My notes

Write what clicked, what confused you, and what you want to try. Saved in this browser only.

Learn it live

Digital Builders University runs small live workshops where you build something real with an instructor beside you, starting with Vibe Coding 101.

See workshops

Practise it

Last reviewed November 25, 2026. AI tools change fast; check each tool’s own site for current features, plans and prices.