Skip to content

JavaScript Promises, async and await Explained with Examples

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Some work takes time: loading exchange rates from a server, reading a file a person picked, or waiting two seconds before hiding a message. JavaScript does not freeze the page while it waits. It starts the job, carries on with the next lines, and comes back when the result is ready. That is great for people using your page, but it means the order your code runs in is not always the order you wrote it. This lesson explains that order, then teaches promises and the async and await keywords, which let you write waiting code that reads from top to bottom. It is the groundwork for the next lesson, where you load real data with fetch.

Quick answer

JavaScript runs one thing at a time and never blocks while waiting; slow jobs finish later and their code runs after the current code ends. A promise is an object that stands for a result that will arrive later: it is pending, then fulfilled with a value or rejected with an error. Read it with .then() and .catch(), or, more simply, inside an async function with const value = await promise;, wrapping the awaits in try and catch to handle errors. await pauses only that function, not the page. Start independent jobs together with await Promise.all([a(), b()]).

35 min read and practice · Content checked on 11 November 2026

In this lesson you will

  • Predict the order in which synchronous and delayed code runs
  • Read and create promises with then, catch and new Promise
  • Write async functions with await and handle errors with try and catch
  • Run independent tasks at the same time with Promise.all

Why JavaScript does not wait

A browser tab runs your JavaScript on a single thread, the same one that draws the page and reacts to clicks. If your code sat and waited three seconds for a server, the whole page would freeze: no scrolling, no typing, no buttons. So slow jobs are handed to the browser, which does them in the background. When one finishes, its follow-up code joins a queue and runs as soon as the current code has finished. This system is called the event loop, and you have already used it: every event listener from the last lesson runs this way.

The simplest delayed job is a timer. setTimeout(fn, 1000) asks the browser to call fn after about 1000 milliseconds, then returns at once. Even setTimeout(fn, 0) runs fn only after the code around it has finished. The delay is a minimum, not a promise of exact timing, and browsers slow timers down in background tabs. For a repeating job, setInterval(fn, ms) calls fn again and again until you stop it with clearInterval.

Try it: Guess the order before you run it
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

Line 2 prints before line 3 even though its timer is 0 ms, because timers wait until the running script has finished. The paragraph changes after a second and a half while the page stays usable.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

What is a promise in JavaScript, in simple words?

It is like an order number at a coffee counter. You get it straight away, you can do other things while you wait, and later it turns into your drink or into an apology that something went wrong. In code, then or await is where you collect the drink.

Should I use then or async and await?

Prefer async and await for most code, because it reads in order and uses normal try and catch. You will still read .then chains in libraries and older code, and a short .then is fine for a single step. Both are built on the same promises.

Does async make JavaScript run on more than one thread?

No. Your code still runs on one thread, one piece at a time. The waiting is done by the browser, for example for a network reply or a timer, and your code resumes when the result is ready. Heavy maths still blocks the page; Web Workers are the tool for that.

What is an unhandled promise rejection?

It is an error from a promise that no catch received. The browser logs it in the console, but the page carries on as if nothing happened, often leaving a spinner turning forever. Add try and catch around awaits, or .catch at the end of every chain.

Is setTimeout accurate to the millisecond?

No. The delay is the shortest time before your function may run. It runs later if other code is busy, and browsers slow timers in hidden tabs to save battery. For clocks and countdowns, read the real time with Date.now() on each tick instead of counting ticks.

Sources

Facts in this lesson were checked against these official pages on 11 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.