2 · Build one skill at a time · 18 MIN
Wait for a promise without blocking JavaScript
Await pauses an async flow until a promise settles.
A promise represents a result that is not available yet. Await suspends this module’s continuation while Node can perform other event-loop work. It does not make CPU-heavy JavaScript non-blocking. The timer below simulates waiting; its delay is a minimum rather than an exact schedule. A try/catch around await handles a rejected promise. Real API code should distinguish expected failures from programming errors and should not hide every failure behind an apparently successful result.
Identify where the promise is created. Follow execution up to await, then after settlement. Trigger the deliberate rejection. Restore the code and compare both flows.
Before you start
Install Node.js 22.12+ (or a supported newer LTS). Create a folder named codingneed-node. Open a terminal in that folder. Save ONE lesson example as lesson.mjs and run node lesson.mjs. No npm dependencies are required. HTTP lessons keep running until you press Ctrl+C; stop the previous server before starting another.
File for this example: lesson.mjs
New words, explained
- promise
- A value representing pending success or failure.
- await
- Resume this async flow when the promise settles.
- rejection
- The failure outcome of a promise.
Follow the example step by step
- Identify where the promise is created.
- Follow execution up to await, then after settlement.
- Trigger the deliberate rejection.
- Restore the code and compare both flows.
You are ready to move on when: Success prints in the expected order. A rejection reaches the catch block. Explain why a long synchronous loop would still block this process.
Read the example
import {setTimeout as delay} from 'node:timers/promises';
async function loadTopic() {
await delay(20);
return 'Node.js';
}
console.log('Starting');
try {
console.log(await loadTopic());
} catch (error) {
console.error('Could not load topic:', error.message);
process.exitCode = 1;
}
console.log('Finished');Check the expected output
Starting, Node.js and Finished print in that order, each on its own line.
Your challenge
Inside loadTopic, throw new Error("Practice failure") after awaiting the delay. Observe the catch message and nonzero exit code, then restore the success case.
Solution cost: Discuss the operations in this small example; rendering and I/O costs depend on the host. time · Proportional to the example’s retained data. space
Common trap
Calling an async function without awaiting it gives a promise, not its eventual value.
Study the project implementation
import {setTimeout as delay} from 'node:timers/promises';
async function loadTopic() {
await delay(20);
return 'Node.js';
}
console.log('Starting');
try {
console.log(await loadTopic());
} catch (error) {
console.error('Could not load topic:', error.message);
process.exitCode = 1;
}
console.log('Finished');Further reading: Official documentation
Next lesson: Read and validate a JSON file →