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.
Console
- 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
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.
- MDN: Introducing asynchronous JavaScript
- MDN: How to use promises
- MDN: Using promises
- MDN: async function
- MDN: await
- MDN: Promise.all()
- MDN: Promise.allSettled()
- MDN: Window setTimeout() method
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.