Skip to content

Instantly share code, notes, and snippets.

@Ephygtz
Created February 16, 2024 15:13
Show Gist options
  • Save Ephygtz/1a0142f60d6bb87a0320cb16b7fb34fd to your computer and use it in GitHub Desktop.
Save Ephygtz/1a0142f60d6bb87a0320cb16b7fb34fd to your computer and use it in GitHub Desktop.
Working with JS Promises
Here are the key points for working with Promises:
A Promise can be pending, fulfilled, or rejected
A Promise is settled if it is either fulfilled or rejected
Use then to get the fulfilled value of a Promise
Use catch to handle errors
Use finally to perform cleanup logic that you need in either the success or error case
Chain Promises together to perform asynchronous tasks in sequence
Use Promise.all to get a Promise that is fulfilled when all given Promises are fulfilled, or rejects when one of those Promises rejects
Use Promise.allSettled to get a Promise that is fulfilled when all given Promises are either fulfilled or rejected
Use Promise.race to get a Promise that is fulfilled or rejected when the first of the given Promises is either fulfilled or rejected
Use Promise.any to get a Promise that is fulfilled when the first of the given Promises is fulfilled
Use the await keyword to wait for the fulfillment value of a Promise
Use a try-catch block to handle errors when using the await keyword
A function that uses await inside of it must use the async keyword
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment