JavaScript promises cheat sheet

JavaScript promises and async/await Cheat Sheet

Alex Alykiotis and Luke Holmquist
English

About

JavaScript promises are one of the most useful features that the language offers us that help with handling asynchronous operations easily.

After reading this cheat sheet, the readers will have a better understanding of how to:

  • Create a new JavaScript promise.
  • Handle the completion of a promise.
  • Catch promise errors.
  • Handle multiple promises at the same time.
  • Understand async/await syntax.

Excerpt

The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
A promise has three states:

Pending: Initial state, neither fulfilled nor rejected.

Fulfilled: The operation was completed successfully.

Rejected: The operation failed.

Promise

Creates a new Promise object. The constructor is primarily used to wrap functions that do not already support promises.

new Promise((resolve, reject) => { setTimeout(() => resolve(), 2000); });