-
-
Save jstarry/9b86287eec9468696d5dd28aa7a6c50a to your computer and use it in GitHub Desktop.
NodeJS async race conditions
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
const fn2 = async (id: number) => { | |
console.log(`[${id}] called fn2`); | |
}; | |
const fn = async (id: number) => { | |
console.log(`[${id}] fn start`); | |
await fn2(id); | |
console.log(`[${id}] fn end`); | |
}; | |
(async () => { | |
await Promise.all(new Array(2).fill(0).map((async (_, id) => { | |
console.log(`[${id}] start`); | |
await fn(id); | |
console.log(`[${id}] finish`); | |
}))); | |
})(); | |
/* | |
Output: | |
[0] start | |
[0] fn start | |
[0] called fn2 | |
[1] start | |
[1] fn start | |
[1] called fn2 | |
[0] fn end | |
[1] fn end | |
[0] finish | |
[1] finish | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment