Learn on the Go. Download the Codeflare Mobile from iOS App Store.
A Promise in JavaScript represents a value that may be:
A promise has three states:
Promise chaining means linking multiple .then() calls together so that the output of one becomes the input of the next.
👉 Instead of nesting:
doSomething(function(result) {
doNext(result, function(nextResult) {
doAnother(nextResult, function(finalResult) {
console.log(finalResult);
});
});
}); 👉 You use chaining:
doSomething()
.then(result => doNext(result))
.then(nextResult => doAnother(nextResult))
.then(finalResult => console.log(finalResult))
.catch(error => console.error(error)); Each .then():
const promise = Promise.resolve(5);
promise
.then(num => num * 2) // 10
.then(num => num + 3) // 13
.then(num => console.log(num)); // 13
👉 Each step transforms the value and passes it forward.
.then(value => value * 2) Automatically wrapped in a resolved promise.
.then(value => {
return new Promise(resolve => {
setTimeout(() => resolve(value * 2), 1000);
});
})
👉 The next .then() waits for it.
fetch('https://api.example.com/user')
.then(response => response.json())
.then(user => {
return fetch(`https://api.example.com/posts/${user.id}`);
})
.then(response => response.json())
.then(posts => console.log(posts))
.catch(error => console.error(error));
👉 Flow:
Errors propagate down the chain until caught.
doSomething()
.then(result => {
throw new Error("Something went wrong");
})
.then(() => {
console.log("This will not run");
})
.catch(err => {
console.error(err.message);
});
👉 A single .catch() can handle errors from all previous .then() calls.
.finally().finally() runs no matter what:
fetchData()
.then(data => console.log(data))
.catch(err => console.error(err))
.finally(() => console.log("Done"));
.then(result => {
fetchData(result); // missing return
})
👉 Fix:
.then(result => {
return fetchData(result);
})
.then() (anti-pattern).then(result => {
fetchData(result).then(data => {
console.log(data);
});
}) 👉 Fix:
.then(result => fetchData(result))
.then(data => console.log(data)); Runs one after another:
task1()
.then(result => task2(result))
.then(result => task3(result));
Runs all at once:
Promise.all([task1(), task2(), task3()])
.then(results => console.log(results)); Promise chaining:
getUser()
.then(user => getPosts(user.id))
.then(posts => console.log(posts))
.catch(err => console.error(err)); Async/Await (cleaner):
async function getData() {
try {
const user = await getUser();
const posts = await getPosts(user.id);
console.log(posts);
} catch (err) {
console.error(err);
}
} 👉 Async/await is just syntactic sugar over promise chaining.
const tasks = [1, 2, 3];
tasks.reduce((promise, task) => {
return promise.then(() => {
return new Promise(resolve => {
setTimeout(() => {
console.log(task);
resolve();
}, 1000);
});
});
}, Promise.resolve());
👉 Executes tasks one after another.
Think of promise chaining like a pipeline:
Input → then() → then() → then() → catch() Each stage:
✔ Always return values or promises in .then()
✔ Use a single .catch() at the end
✔ Avoid nested .then()
✔ Use .finally() for cleanup
✔ Prefer async/await for readability in complex flows
Promise chaining is what made JavaScript move from messy callbacks to structured async programming. It’s the foundation behind:
Latest tech news and coding tips.
The software industry is one of the most competitive places to build a career. Every…
How to Build APIs That Are Easy to Use, Scale, and Maintain Learn on the…
Almost everyone starts learning JavaScript with the wrong expectations. Let's fix them. Download the Codeflare…
Phaser JS is a powerful, open-source HTML5 game development framework used for creating 2D games that…
JavaScript / Node.js Authentication Libraries 1. Passport.js One of the most popular authentication middleware libraries…
Every profession comes with its own set of tools. A carpenter has a toolbox, a…