📖 Introduction
If you’re learning JavaScript or working on modern web projects, Promise chaining is a must-know concept. It allows you to handle sequential asynchronous tasks like fetching data, processing responses, or handling errors — all in a clean, readable format.
Whether you’re calling APIs, validating forms, or controlling animations, these 17 promise chaining examples will guide you step-by-step with real-world use cases.
✅ Recommended Reading: Don’t miss our full guide on Asynchronous JavaScript: Promises, Async/Await, and Callbacks to build a solid foundation.
🔁 What is Promise Chaining?
Promise chaining is the process of linking multiple .then()
calls, where each step waits for the result of the previous one.
getData()
.then(data => process(data))
.then(result => show(result))
.catch(error => console.error(error));
This pattern makes your code easier to read, debug, and maintain — compared to callback hell.
💡 Top 17 JavaScript Promise Chaining Examples
Each example here is written for developers like you — clear, concise, and useful.
1. ✅ Basic Chaining
Promise.resolve(5)
.then(val => val + 5)
.then(console.log); // 10
2.🌐 Fetch Data and Process
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(res => res.json())
.then(data => console.log("Title:", data.title));
3.❌ Error Handling
Promise.resolve()
.then(() => { throw new Error("Oops!"); })
.catch(err => console.error("Caught:", err.message));
4.🕒 Delayed Chaining
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
delay(1000)
.then(() => console.log("1 second"))
.then(() => delay(500))
.then(() => console.log("Another 0.5 seconds"));
5.🔁 API Chaining
fetch('/user')
.then(res => res.json())
.then(user => fetch(`/posts?userId=${user.id}`))
.then(res => res.json())
.then(posts => console.log(posts));
6. 🔂 Function Reuse
function square(x) {
return Promise.resolve(x * x);
}
square(2).then(square).then(console.log); // 16
7.🎬 Sequential Actions
function log(msg, delay) {
return new Promise(resolve => setTimeout(() => {
console.log(msg); resolve();
}, delay));
}
log("First", 1000)
.then(() => log("Second", 1000))
.then(() => log("Third", 1000));
8.🧮 Arithmetic Chaining
Promise.resolve(5)
.then(n => n * 2)
.then(n => n + 3)
.then(console.log); // 13
9.✅ Form Field Validation
function validate(field, value) {
return value ? Promise.resolve(value) : Promise.reject(`${field} is invalid`);
}
validate("Email", "test@mail.com")
.then(val => validate("Name", "Arsalan"))
.then(() => console.log("Valid"))
.catch(err => console.error(err));
10. 📤 Upload → Process → Notify
uploadFile()
.then(file => processFile(file))
.then(result => notifyUser(result))
.catch(console.error);
11.🔗 Dependent Calls
getUser()
.then(user => getPosts(user.id))
.then(posts => console.log(posts));
12.🛠 Mid-Chain Catch
Promise.resolve()
.then(() => { throw new Error("Fail"); })
.catch(err => "Recovered")
.then(console.log); // "Recovered"
13.🧵 Flattening Nesting
Promise.resolve("Start")
.then(msg => Promise.resolve(msg + " → Step 1"))
.then(msg => msg + " → Step 2")
.then(console.log);
14.🤖 Conditional Chain
Promise.resolve(7)
.then(num => num > 5 ? "Large" : "Small")
.then(console.log);
15.🔁 Sync + Async Mix
Promise.resolve("Loading")
.then(str => str + "...")
.then(str => new Promise(resolve => setTimeout(() => resolve(str + " Done!"), 1000)))
.then(console.log);
16.🔐 Logged-in Check
Promise.resolve(true)
.then(loggedIn => loggedIn ? fetch('/data') : Promise.reject("Not Authenticated"))
.then(res => res.json())
.then(console.log)
.catch(console.error);
🔄 Final Cleanup
Promise.reject("Something went wrong")
.catch(err => console.error(err))
.then(() => console.log("Cleanup complete"));
🧠 Why Promise Chaining is Powerful
- 🔄 Clean and readable async code
- 💥 Centralized error handling with
.catch()
- ⏳ Better control over execution flow
- 🔄 Reusable and maintainable functions
📘 Continue Learning
To go deeper into async handling, check out this full guide:
👉 Asynchronous JavaScript: Promises, Async/Await, and Callbacks
🔗 References
📣 Final Words
These examples show you exactly how Promise chaining works in JavaScript — not just with dummy numbers, but in real use cases like fetching APIs, validation, and sequencing logic. Practice these snippets and you’ll start writing cleaner async code in no time.
If this helped, share the article and let me know on Makemychance.com!
Arsalan Malik is a passionate Software Engineer and the Founder of Makemychance.com. A proud CDAC-qualified developer, Arsalan specializes in full-stack web development, with expertise in technologies like Node.js, PHP, WordPress, React, and modern CSS frameworks.
He actively shares his knowledge and insights with the developer community on platforms like Dev.to and engages with professionals worldwide through LinkedIn.
Arsalan believes in building real-world projects that not only solve problems but also educate and empower users. His mission is to make technology simple, accessible, and impactful for everyone.