How to handle the result of a resolved Promise?
How to handle the result of a resolved Promise?
27026-Sep-2023
Updated on 26-Sep-2023
Home / DeveloperSection / Forums / How to handle the result of a resolved Promise?
How to handle the result of a resolved Promise?
Aryan Kumar
26-Sep-2023To handle the result of a resolved Promise in JavaScript, you can use the .then() method. The .then() method allows you to specify a callback function that will be executed when the Promise is resolved successfully (i.e., in the fulfilled state). Here's how you can handle the result of a resolved Promise:
In this example, when myPromise is resolved (after a 2-second delay), the .then() method's callback function is called, and you can access the resolved value (in this case, the string "Operation succeeded!") as the result parameter within the callback.
You can also chain multiple .then() methods together for handling multiple asynchronous operations in sequence or for creating more complex Promise chains.
Here's an example with chaining:
In this chained example, the second .then() handler is called with the result of the first Promise, and you can continue processing or returning new Promises as needed. This chaining mechanism is one of the strengths of Promises in managing complex asynchronous flows.