Cheat Sheet

JavaScript promises and async/await Cheat Sheet

By Alex Alykiotis and Luke Holmquist
May 4, 2021

JavaScript promises cheat sheet cover

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.
  • Async/await syntax.

Cheat Sheet 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); });

Want to see more? Get the full cheat sheet.

Tags: javascript

Comments