Promises

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 ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.