Created
June 13, 2021 20:41
-
-
Save nkhil/84f0b30e762cbad0a53c09c8fdce5b79 to your computer and use it in GitHub Desktop.
Do not use async/await inside forEach functions
This file contains hidden or 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 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