Can use multiple .then blocks for a single Promise? If so, in what order do they execute?
Can use multiple .then blocks for a single Promise? If so, in what order do they execute?
14026-Sep-2023
Updated on 26-Sep-2023
Home / DeveloperSection / Forums / Can use multiple .then blocks for a single Promise? If so, in what order do they execute?
Can use multiple .then blocks for a single Promise? If so, in what order do they execute?
Aryan Kumar
26-Sep-2023Yes, 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.