Skip to content

Repository files navigation

πŸš€ Advanced Promises in Node.js

Course Language Runtime Category Level Focus Purpose Style

Promise Combinators & Custom Promise Wrappers (Backend-Focused)

A visual, example-driven, backend-oriented repository that teaches Promise combinators and Custom Promise Wrappers through clean, runnable Node.js examples.

This repo is structured so that:

  • πŸ“– You can understand everything by reading the README alone
  • ▢️ You can run each example independently
  • 🧠 You clearly see why and when each pattern is used in real backend systems

🧠 Learning Goals

By the end of this repository, you will understand:

  • How Promise combinators actually behave at runtime
  • The strengths and limits of each combinator
  • Why combinators alone are not sufficient for backend control
  • What Custom Promise Wrappers are
  • How wrappers are built on top of combinators
  • How these patterns appear in production-grade Node.js backends

▢️ How to Run & Use This Repository

This repository is example-driven.
Each folder contains a standalone runnable example.


πŸ”§ Prerequisites

Make sure you have:

  • Node.js v18+ (recommended: latest LTS)
  • npm

Check versions:

node -v
npm -v

πŸ“₯ Installation

Clone the repository:

git clone https://github.com/Maryam-Skaik/nodejs-promise-patterns.git
cd nodejs-promise-patterns

Install dependencies:

npm install

ℹ️ axios is used to simulate real HTTP requests.


πŸ“ Repository Structure

nodejs-promise-patterns/
β”‚
β”œβ”€β”€ package.json
β”œβ”€β”€ package-lock.json
β”‚
β”œβ”€β”€ examples/
β”‚   β”‚
β”‚   β”œβ”€β”€ 01-promise-all/
β”‚   β”‚   └── index.js
β”‚   β”‚
β”‚   β”œβ”€β”€ 02-promise-race/
β”‚   β”‚   └── index.js        # timeout + fastest winner
β”‚   β”‚
β”‚   β”œβ”€β”€ 03-promise-allSettled/
β”‚   β”‚   └── index.js        # batch processing + partial failure
β”‚   β”‚
β”‚   β”œβ”€β”€ 04-promise-any/
β”‚   β”‚   └── index.js        # fallback services
β”‚   β”‚
β”‚   β”œβ”€β”€ 05-wrapper-timeout/
β”‚   β”‚   └── index.js
β”‚   β”‚
β”‚   β”œβ”€β”€ 06-wrapper-retry/
β”‚   β”‚   └── index.js
β”‚   β”‚
β”‚   β”œβ”€β”€ 07-wrapper-fallback/
β”‚   β”‚   └── index.js
β”‚   β”‚
β”‚   └── 08-wrapper-fail-fast/
β”‚       └── index.js
β”‚
└── README.md

Each folder:

  • Contains one clear concept
  • Is clean, minimal, and runnable
  • Is written in a teaching-friendly style

▢️ Running an Example

From the project root:

node 01-promise-all/index.js

Run any other example by changing the folder name:

node 05-wrapper-timeout/index.js

🎯 How to Study This Repository (Recommended)

  1. Start with Promise Combinators 01 β†’ 04
  2. Understand why they are insufficient alone
  3. Move to Custom Promise Wrappers 05 β†’ 08
  4. Modify examples: - Change URLs - Adjust timeouts - Increase retry count

1️⃣ Promise Basics (Quick Refresh)

A Promise represents an asynchronous operation that can be:

PENDING β†’ RESOLVED
        β†’ REJECTED

In Node.js backend development, promises appear everywhere:

  • 🌐 HTTP requests
  • πŸ—„οΈ Database queries
  • πŸ“‚ File system operations
  • πŸ”Œ External services

This repository assumes you already know async / await.


2️⃣ Promise Combinators

Promise combinators are built-in coordination tools.

πŸ‘‰ They decide how multiple promises settle together

πŸ‘‰ They do not enforce business rules


πŸ”Ή Promise.all

πŸ“‚ 01-promise-all/index.js

Rule

βœ… resolve only if ALL promises resolve

❌ reject immediately if ANY promise rejects

Mental Model

[A βœ…] [B βœ…] [C ❌]
        ↓
     ❌ reject

When to use:

  • Every operation is mandatory
  • Partial success is useless

πŸ”Ή Promise.race

πŸ“‚ 02-promise-race/index.js

Rule

  • First promise to settle wins (resolve OR reject)

Mental Model

[A 🐒] [B ⚑]
        ↓
     B wins

When to use

  • ⏱️ Timeouts
  • Fastest response wins

This example demonstrates:

  • Racing an API call against a timeout
  • How rejection can win the race

πŸ”Ή Promise.allSettled

πŸ“‚ 03-promise-allSettled/index.js

Rule

  • Waits for ALL promises
  • Returns both resolve and reject results

Mental Model

[A βœ…] [B ❌] [C βœ…]
        ↓
   ALL results

When to use

  • Batch jobs
  • Logging failures
  • Partial success is acceptable

πŸ”Ή Promise.any

πŸ“‚ 04-promise-any/index.js

Rule

  • First resolve wins
  • Rejects only if ALL promises reject

Mental Model

[A ❌] [B ❌] [C βœ…]
        ↓
     C wins

When to use

  • Fallback services
  • Redundant APIs
  • High availability

3️⃣ Why Promise Combinators Are NOT Enough

Combinators answer:

β€œHow do promises settle together?”

They do NOT answer:

  • ⏱️ How long is too long?
  • πŸ” Should we retry?
  • πŸ›‘ Should we fail fast?
  • πŸ”„ Should we fallback?

πŸ‘‰ Backend systems need policy + control, not just coordination.


4️⃣ Custom Promise Wrappers

A Custom Promise Wrapper is:

A function that takes a promise (or a function returning a promise), applies rules, and returns a new controlled promise.

Original Promise
      ↓
   Wrapper Logic
      ↓
 Controlled Promise

Wrappers enforce backend behavior and guarantees.


5️⃣ Wrapper Examples

⏱️ Timeout Wrapper

πŸ“‚ 05-wrapper-timeout/index.js

Goal

  • Enforce a maximum execution time

Technique

  • Race the original promise against a timer

🧠 Uses: Promise.race


πŸ” Retry Wrapper

πŸ“‚ 06-wrapper-retry/index.js

Goal

  • Retry transient failures (network, temporary DB issues)

Technique

  • Re-execute promise function with retry limits

🧠 Uses: controlled re-execution + async/await


πŸ”„ Fallback Wrapper

πŸ“‚ 07-wrapper-fallback/index.js

Goal

  • Use alternative services when one fails

Technique

  • Try multiple promises, accept first resolve

🧠 Uses: Promise.any


πŸ›‘ Fail-Fast Wrapper

πŸ“‚ 08-wrapper-fail-fast/index.js

Goal

  • Abort immediately if any critical operation fails

Technique

  • Force strict success requirement

🧠 Uses: Promise.all


6️⃣ Wrappers + Combinators = Power πŸ’₯

Wrapper Goal Underlying Combinator
Timeout Promise.race
Retry allSettled / loops
Fallback Promise.any
Fail-Fast Promise.all

πŸ”© Combinators = low-level coordination

🧠 Wrappers = application-level control


7️⃣ Real Backend Scenarios

βœ” External APIs

βœ” Microservices

βœ” Batch processing

βœ” Fault-tolerant systems

βœ” SLA enforcement


🧾 Final Takeaway

🧩 Promise combinators define how promises settle

🧠 Custom Promise Wrappers define how your backend behaves

πŸš€ Professional Node.js systems rely on both together

This repository is suitable for:

  • Teaching
  • Mentorship
  • Interview preparation
  • Real-world backend reference

About

Example-driven Node.js repository demonstrating Promise combinators and custom Promise wrappers for backend control, fault tolerance, and async orchestration.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages