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.
A SOC Analyst (Security Operations Center Analyst) is one of the frontline defenders of an organization’s cybersecurity…
Most image upload systems assume one thing: the user has a stable internet connection. That assumption…
Imagine a user submits a form while their internet connection suddenly disappears. Normally, the request…
Cybercriminals don't always announce their presence. Many compromises are designed to remain unnoticed for weeks…
For years, jQuery was everywhere. If you were building websites in the 2010s, there was a…
Few things halt a developer’s flow faster than seeing the dreaded word: CONFLICT. A Git merge…