The then() method in Promises serves two primary purposes:
Handling the Success of a Promise:
The main purpose of the then() method is to specify a callback function that will be executed when the Promise is resolved successfully (i.e., when it transitions to the fulfilled state).
This callback function receives the resolved value of the Promise as its argument, allowing you to work with the result of the asynchronous operation.
Chaining Promises:
Another key purpose of then() is to facilitate chaining Promises together, enabling you to sequence multiple asynchronous operations in a clean and organized manner.
When you return a new Promise (or any value) from the then() callback, it creates a new Promise that represents the result of the callback function. This allows you to chain Promises one after the other.
Here's how the then() method works:
You call then() on a Promise object and provide one or two callback functions:
The first callback function is executed when the Promise is resolved successfully. It takes one argument, which is the resolved value of the Promise.
The second callback function is optional and is executed when the Promise is rejected. It takes one argument, which is the rejection reason.
The then() method returns a new Promise. This new Promise will be in one of the following states, depending on what happens in the callback function:
If the callback function returns a value, the new Promise will be resolved with that value.
If the callback function throws an exception or returns a rejected Promise, the new Promise will be rejected with the exception or rejection reason.
Here's an example illustrating how then() works for handling success:
In this example, when myPromise is resolved successfully, the callback function provided to
.then() is executed with the resolved value, allowing you to handle the success of the asynchronous operation.
And here's an example demonstrating chaining Promises:
In this chained example, the then() method is used to chain two asynchronous operations together, ensuring that the second operation waits for the first one to complete before executing.
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.
The then() method in Promises serves two primary purposes:
Handling the Success of a Promise:
Chaining Promises:
Here's how the then() method works:
You call then() on a Promise object and provide one or two callback functions:
The then() method returns a new Promise. This new Promise will be in one of the following states, depending on what happens in the callback function:
Here's an example illustrating how then() works for handling success:
In this example, when myPromise is resolved successfully, the callback function provided to .then() is executed with the resolved value, allowing you to handle the success of the asynchronous operation.
And here's an example demonstrating chaining Promises:
In this chained example, the then() method is used to chain two asynchronous operations together, ensuring that the second operation waits for the first one to complete before executing.