Asynchronous JavaScript Promises

Asynchronous callbacks are technically enough to implement all kinds of asynchronous logic, but they have some major issues that make them hard to work with. Promises is a relatively new JavaScript feature that allow us to represent an operation that is expected to complete some time in the future and promises to give us either a value on completion or an error on failure.

The Promise itself has three explicitly defined states:

  • Pending: This is the initial state of the Promise and signifies a Promise that is neither fulfilled nor rejected.
  • Fulfilled: This state signifies that the asynchronous operation completed without errors.
  • Rejected: This state signifies that the asynchronous operation failed.

A Promise can go from the a pending state to a fulfilled state by resolving the Promise with an optional value. We can transition to a rejected failure state by rejecting with an optional error.