Promises & async/await
IntermediateHow a promise stands in for a result that isn't ready yet, when its callbacks run, and what await really pauses.Builds on Callback Hell, Event Loop
Empty
Empty
Empty
Empty
- Running code
- Running microtasks
- Taking the next task
- Waiting
Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
2 min read
On this page
What is a promise in JavaScript?
A promise is an object that stands in for a result that is not ready yet. It is in one of three states: (still waiting), fulfilled (it has a value) or rejected (it has an error). Once it settles, it never changes again.
async/await is syntax on top of promises. An async function always returns a promise, and
await pauses that function, and only that function, until a promise settles.
How do promises and async/await run?
.then(fn)registersfnto run with the value once the promise is fulfilled;.catch(fn)runs on rejection;.finally(fn)runs either way..thenreturns a new promise, so steps chain flat, one after another, instead of nesting.- Promise callbacks and the code after an
awaitrun as microtasks: after the current code, before the next timer or event. - To run independent jobs at the same time, start them together and combine them:
Promise.all(all must succeed),Promise.allSettled(wait for every result),Promise.race(first to settle) andPromise.any(first to succeed).
What mistakes do people make with promises?
Mini example
async function showUser(id) { try { const response = await fetch(`/api/users/${id}`); const user = await response.json(); console.log(user.name); } catch (error) { console.error('Could not load the user', error); }} const [a, b] = await Promise.all([fetch('/api/a'), fetch('/api/b')]);Quick check
Inside an async function, what does await pause?
Further reading
Watch the video
The full explanation from the YouTurkey Tech YouTube channel, in Arabic.
All steps as text
- Step 1:
The script starts on the call stack.
waitandbreware declared but not called yet. - Step 2:
start is printed.
Line 12
- Step 3:
brew()is called. An async function runs synchronously until its firstawait.Line 13
- Step 4:
boiling water is printed.
Line 6
- Step 5:
wait(1000)is called and goes on top of the stack.Line 7
- Step 6:
new Promiseruns its function right away. It starts a timer that will callresolvein 1000 ms.Line 2
- Step 7:
waitreturns a promise that is still pending: a placeholder for a result that is not ready yet.Line 2
- Step 8:
awaitmeets a pending promise, sobrewpauses and leaves the stack. It hands its caller a pending promise of its own.Line 7
- Step 9:
.thenregisters a callback on brew's promise. Nothing is queued yet: it runs only once that promise is fulfilled.Line 13
- Step 10:
end is printed.
awaitpaused onlybrew, never the rest of the script.Line 14
- Step 11:
The script is done and the stack is empty. The loop waits for the timer.
- Step 12:
After 1000 ms,
resolvegoes through the task queue and onto the stack.Line 2
- Step 13:
Calling
resolvefulfills the promise fromwait. The rest ofbrew, after theawait, is queued as a microtask.Lines 2–7
- Step 14:
The stack is empty, so microtasks run:
brewresumes exactly where it paused.Line 8
- Step 15:
coffee ready is printed.
Line 8
- Step 16:
return 'coffee'fulfills brew's promise with 'coffee', so the.thencallback is queued as a microtask.Lines 9–13
- Step 17:
The loop is still emptying microtasks, so the
.thencallback runs at once, withdrinkset to 'coffee'.Line 13
- Step 18:
enjoy coffee is printed.
Line 13
- Step 19:
The callback returns. The stack and both queues are empty.
Line 13
- Step 20:
Output: start, boiling water, end, coffee ready, enjoy coffee. No nesting: each step that waits is one flat line.