Chapter 10: Node.js Promises

Promises in Node.js — written as if I’m sitting next to you, explaining everything step by step with real examples, analogies, common mistakes, and the mental models that actually help people understand and use Promises correctly.

Let’s go slowly and build it properly.

1. What is a Promise really? (The honest explanation)

A Promise is a placeholder object that represents a future value.

It’s JavaScript’s built-in way of saying:

“I’m going to do something that takes time. I don’t have the result right now, but when I do, I will either:

  • give you the successful result, or
  • tell you what went wrong.”

So instead of waiting (blocking), you get a Promise object immediately, and you attach instructions saying “when you finish, please do this”.

This is the foundation of modern asynchronous JavaScript in Node.js.

2. Three possible states of a Promise

State Meaning Can change? Final?
pending The operation is still running Yes No
fulfilled The operation succeeded → has a value No Yes
rejected The operation failed → has a reason (error) No Yes

Once a Promise is fulfilled or rejected, it is settled — it can never change state again.

3. Creating a Promise (the basic syntax)

JavaScript
  • resolve(value) → promise becomes fulfilled with that value
  • reject(reason) → promise becomes rejected with that reason (usually an Error)

4. The most important way to use a Promise: .then() and .catch()

JavaScript

Realistic output after 1.5 seconds:

text

or if rejected:

text

5. Real Node.js example – using the built-in Promise-based fs

JavaScript

Output order:

text

This is non-blocking — the program keeps running while the file is being read.

6. The modern & recommended way: async / await + Promises

async / await is syntactic sugar on top of Promises — it makes them look almost synchronous.

JavaScript

Very important mental model:

text

The rest of your application (event loop) keeps running while we wait.

7. Chaining multiple Promises (very common pattern)

JavaScript

This looks clean, but it waits sequentially — one request finishes before the next starts.

8. Running multiple Promises in parallel (much faster)

JavaScript

Key points:

  • Promise.all waits for all promises to settle
  • If any promise rejects → whole Promise.all rejects
  • Much faster when operations are independent

9. Other very useful Promise static methods (2025–2026)

Method What it does When to use it
Promise.all() Wait for all to succeed (or first failure) Parallel independent tasks
Promise.allSettled() Wait for all to finish (success or failure) You want results even if some fail
Promise.race() Returns the first promise that settles Timeouts, fastest server response
Promise.any() Returns the first fulfilled promise (ignores rejections) Try multiple sources, take first success

Example – allSettled is very popular in production:

JavaScript

10. Common mistakes people make with Promises

Mistake What happens Fix
Forgetting to return await Silent bugs, wrong order return await something or just return something
Not handling rejection Unhandled promise rejection → crash Always .catch() or try/catch
Using .then inside async function Harder to read Prefer await
Chaining many .then without error catch Errors disappear in chain Add .catch at the end or use try/catch
Doing CPU heavy work inside .then Still blocks event loop Move to Worker Threads

Summary – Quick mental checklist

  • Promise = future value container (pending → fulfilled / rejected)
  • Use async / await + try/catch for most new code
  • Use Promise.all when tasks are independent and you want speed
  • Use Promise.allSettled when you need to know what failed
  • Always handle errors — unhandled rejections crash Node.js
  • await only pauses the current function, not the whole app

Would you like to go deeper into any of these areas?

  • Converting old callback code to Promises / async-await
  • Real Express route with proper Promise handling & error middleware
  • Timeout pattern using Promise.race
  • How to debug hanging / unresolved Promises
  • Combining Promises with streams or events
  • Common Promise anti-patterns in production

Just tell me what you want to see next — I’ll continue with full, realistic examples. 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *