Last active
June 1, 2020 07:13
-
-
Save itaditya/bd7a0652ba0fa11ef5b6e60922fa156a to your computer and use it in GitHub Desktop.
Snippet for Avoiding the async/await hell medium article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
this is a cool solution @itaditya, I'm going to fork this gist!