After seeing how the use of callbacks can lead to some maintainability problems, we are now going to learn about promises and how they can be used to write better asynchronous code. The core idea behind promises is that a promise represents the result of an asynchronous operation. A promise must be in one of the following three states:
- Pending: The initial state of a promise.
- Fulfilled/resolved: The state of a promise representing a successful operation. The terms "fulfilled" and "resolved" are both commonly used to refer to this state.
- Rejected: The state of a promise representing a failed operation.
Once a promise is fulfilled or rejected, its state can never change again. Let's look at the basic syntax of a promise:
function ...