What are Promises?
How Promises Work:
When you create a promise, it starts in a "pending" state. Once the executor function has completed its task, it can either "resolve" the promise (if the task was successful) or "reject" it (if the task encountered an error). If the promise is resolved, it moves into a "fulfilled" state and any code listening for the promise to be fulfilled will be executed. If the promise is rejected, it moves into a "rejected" state and any code listening for the promise to be rejected will be executed.
then() method is used to handle the resolved value of a promise. It takes two arguments, a callback function to execute if the promise is fulfilled, and a callback function to execute if the promise is rejected. You can chain multiple
.then() methods together to handle the promise value at different stages in your code.
catch() method is used to handle a rejected promise. It takes one argument, a callback function to execute if the promise is rejected.
finally() method is used to execute a callback function regardless of whether the promise is fulfilled or rejected. This can be useful for cleaning up resources or performing other tasks that need to be done regardless of the promise's outcome.