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.
Printing a document in JavaScript usually means triggering the browser’s print dialog and controlling what…
The display property controls how an element behaves in the layout and how its children are arranged. Access software…
JavaScript is one of the most flexible programming languages ever created. That flexibility is powerful,…
What is Steam Locomotive (sl)? Steam Locomotive (sl) is a small terminal program on Unix/Linux systems…
What is Rate Limiting? Download this article as a PDF on the Codeflare Mobile App…
Download the Codeflare iOS app and learn on the Go 1. What UI and UX…