Yes, you can use multiple .then blocks for a single Promise in JavaScript. These
.then blocks are typically used to specify different actions or callbacks to be executed when the Promise resolves successfully, allowing you to chain asynchronous operations together in a readable and maintainable way.
The order in which the .then blocks execute is determined by the order in which they are defined in your code. Each
.then block is executed sequentially, one after the other, following the order in which they appear in your code.
Here's an example to illustrate the execution order of multiple .then blocks:
const myPromise = new Promise((resolve, reject) => {
// Simulate an asynchronous operation
setTimeout(() => {
resolve(42); // Resolve the Promise with a value of 42
}, 1000);
});
myPromise
.then((result) => {
console.log("First .then:", result); // Output: First .then: 42
return result * 2;
})
.then((result) => {
console.log("Second .then:", result); // Output: Second .then: 84
return result + 10;
})
.then((result) => {
console.log("Third .then:", result); // Output: Third .then: 94
});
In this example:
The first .then block logs the result (42) and returns a new value (42 * 2 = 84).
The second .then block logs the updated result (84) and returns another new value (84 + 10 = 94).
The third .then block logs the final result (94).
The key point is that the order of execution is sequential and follows the order of your
.then blocks. This chaining of .then blocks is a powerful feature of Promises and allows you to create a sequence of asynchronous operations that depend on each other's results.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Yes, you can use multiple .then blocks for a single Promise in JavaScript. These .then blocks are typically used to specify different actions or callbacks to be executed when the Promise resolves successfully, allowing you to chain asynchronous operations together in a readable and maintainable way.
The order in which the .then blocks execute is determined by the order in which they are defined in your code. Each .then block is executed sequentially, one after the other, following the order in which they appear in your code.
Here's an example to illustrate the execution order of multiple .then blocks:
In this example:
The first .then block logs the result (42) and returns a new value (42 * 2 = 84).
The second .then block logs the updated result (84) and returns another new value (84 + 10 = 94).
The third .then block logs the final result (94).
The key point is that the order of execution is sequential and follows the order of your .then blocks. This chaining of .then blocks is a powerful feature of Promises and allows you to create a sequence of asynchronous operations that depend on each other's results.