Mastering promises
A promise functions as a state machine, symbolizing the eventual success or failure of an asynchronous operation, along with its resultant value. It can exist in any of three states: pending, fulfilled, or rejected.
When a promise is created, it is in the pending state. When a promise is fulfilled, it is in the fulfilled state. When a promise is rejected, it is in the rejected state.
The following diagram shows the various states of a promise and the connections between them:
Figure 4.1 – Attributions and copyright licensing by Mozilla Contributors is licensed under CC-BY-SA 2.5. https://developer.mozilla.org/en-US/docs/MDN/Writing_guidelines/Attrib_copyright_license
After a promise is fulfilled or rejected, it becomes unchangeable. To manage fulfillment, the then
method is employed, while the catch
method is used to address the rejection of the promise.
Now that it is clear what the promises are and how the states are...