Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JonathanLoscalzo/45b82d81de636a01273e71d43005a917 to your computer and use it in GitHub Desktop.
Save JonathanLoscalzo/45b82d81de636a01273e71d43005a917 to your computer and use it in GitHub Desktop.
Snippet for Avoiding the async/await hell medium article
async function orderItems() {
const items = await getCartItems() // async call
const noOfItems = items.length
const promises = []
for(var i = 0; i < noOfItems; i++) {
const orderPromise = sendRequest(items[i]) // async call
promises.push(orderPromise) // sync call
}
await Promise.all(promises) // async call
}
// Although I prefer it this way
async function orderItems() {
const items = await getCartItems() // async call
const promises = items.map((item) => sendRequest(item))
await Promise.all(promises) // async call
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment