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: Also known as resolved, this 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: ...

Get Hands-On Functional Programming with TypeScript now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.