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
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
This repository is example-driven.
Each folder contains a standalone runnable example.
Make sure you have:
- Node.js v18+ (recommended: latest LTS)
- npm
Check versions:
node -v
npm -vClone the repository:
git clone https://github.com/Maryam-Skaik/nodejs-promise-patterns.git
cd nodejs-promise-patternsInstall dependencies:
npm installβΉοΈ
axiosis used to simulate real HTTP requests.
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.mdEach folder:
- Contains one clear concept
- Is clean, minimal, and runnable
- Is written in a teaching-friendly style
From the project root:
node 01-promise-all/index.jsRun any other example by changing the folder name:
node 05-wrapper-timeout/index.js- Start with Promise Combinators
01 β 04 - Understand why they are insufficient alone
- Move to Custom Promise Wrappers
05 β 08 - Modify examples: - Change URLs - Adjust timeouts - Increase retry count
A Promise represents an asynchronous operation that can be:
PENDING β RESOLVED
β REJECTEDIn Node.js backend development, promises appear everywhere:
- π HTTP requests
- ποΈ Database queries
- π File system operations
- π External services
This repository assumes you already know async / await.
Promise combinators are built-in coordination tools.
π They decide how multiple promises settle together
π They do not enforce business rules
Rule
β resolve only if ALL promises resolve
β reject immediately if ANY promise rejects
Mental Model
[A β
] [B β
] [C β]
β
β rejectWhen to use:
- Every operation is mandatory
- Partial success is useless
Rule
- First promise to settle wins (resolve OR reject)
Mental Model
[A π’] [B β‘]
β
B winsWhen to use
- β±οΈ Timeouts
- Fastest response wins
This example demonstrates:
- Racing an API call against a timeout
- How rejection can win the race
π 03-promise-allSettled/index.js
Rule
- Waits for ALL promises
- Returns both resolve and reject results
Mental Model
[A β
] [B β] [C β
]
β
ALL resultsWhen to use
- Batch jobs
- Logging failures
- Partial success is acceptable
Rule
- First resolve wins
- Rejects only if ALL promises reject
Mental Model
[A β] [B β] [C β
]
β
C winsWhen to use
- Fallback services
- Redundant APIs
- High availability
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.
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 PromiseWrappers enforce backend behavior and guarantees.
π 05-wrapper-timeout/index.js
Goal
- Enforce a maximum execution time
Technique
- Race the original promise against a timer
π§ Uses: Promise.race
π 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
π 07-wrapper-fallback/index.js
Goal
- Use alternative services when one fails
Technique
- Try multiple promises, accept first resolve
π§ Uses: Promise.any
π 08-wrapper-fail-fast/index.js
Goal
- Abort immediately if any critical operation fails
Technique
- Force strict success requirement
π§ Uses: Promise.all
| 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
β 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