Skip to content

Instantly share code, notes, and snippets.

@nkhil
Created June 13, 2021 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkhil/84f0b30e762cbad0a53c09c8fdce5b79 to your computer and use it in GitHub Desktop.
Save nkhil/84f0b30e762cbad0a53c09c8fdce5b79 to your computer and use it in GitHub Desktop.
Do not use async/await inside forEach functions
async function someAsyncFuncion(num) {
return await new Promise(resolve => resolve(num))
}
function orchestrator() {
[1, 2, 3, 4, 5].forEach(async num => {
console.log('console 1 fired')
const promiseResolvedValue = await someAsyncFuncion(num)
console.log(promiseResolvedValue)
console.log('console 2 fired')
if (typeof promiseResolvedValue !== 'number') {
throw new Error()
}
})
}
orchestrator()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment