Node.js Async Programming

Callbacks, promises, async/await and the event loop.

✍️ 2 exercises in this lesson

Node runs JavaScript on a single thread. Slow work (disk, network) is handed off, and the event loop runs your callback when it finishes, so the server keeps handling other requests.

  • Old style: error-first callbacks (err, data) => {}
  • Modern: promises with async/await
  • Promise.all() runs independent tasks in parallel.
Never block the event loop with heavy synchronous work (e.g. readFileSync in a request handler or long loops); it freezes every request.

Key Takeaways: Node.js Async Programming

Mastering Node.js Async Programming helps you write clean and reliable Node.js code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Node.js Async Programming Code Example

Node.js
const wait = (ms, value) => new Promise(r => setTimeout(() => r(value), ms));

console.log("1. start");
setTimeout(() => console.log("4. timeout callback"), 0);
Promise.resolve().then(() => console.log("3. promise callback"));
console.log("2. end of script");

const start = Date.now();
const results = await Promise.all([wait(300, "users"), wait(300, "orders")]);
console.log(results, `${Math.round((Date.now() - start) / 100) * 100}ms`);
Output
1. start
2. end of script
3. promise callback
4. timeout callback
[ 'users', 'orders' ] 300ms
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

Node.js Async Programming Practice Exercises

Check what you learned in “Node.js Async Programming”.

/ 2 solved
Fill in the blank

Run two promises in parallel and wait for both.

const [a, b] = await Promise.([p1, p2]);
✓ Correct! Not quite. Try again.
Multiple choice

What should you avoid in a request handler?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services