Handlers
Explore how to assign handlers to JavaScript promises using then, catch, and finally methods. Understand how to manage promise fulfillment, rejection, and completion clearly for effective asynchronous programming.
Assigning handlers with then()
The then() method is present on all promises and takes two arguments. The first argument is a function to call when the promise is fulfilled, called the fulfillment handler. Any additional data related to the asynchronous operation is passed to this function.
The second argument is a function to call when the promise is rejected, called the rejection handler. Similar to the fulfillment handler, the rejection handler is passed any additional data related to the rejection.
Note: Any object that implements the
then()method in this way is called a thenable. All promises are thenables, but not all thenables are promises.
Let’s understand this with the following illustration.
Both ...