So I challenged someone in a code review to prove that their code that made use of async functions wouldn't be susceptible to a race condition. To that end, I came up with some very trivial code examples to demonstrate the issue, worth trying to write down the output and line orderings before you read on. let list; async function clearList () { list = []; // A } async function processList (processList) { await clearList(); // B list = list.concat(processList); // C } processList([1,2,3]); // D processList([4,5,6]) // E .then(() = { console.dir(list); // F }) So the two questions here are what is the output of this code and what order do the lines of code get executed in? Now the point of the code was that it would show that the output is[1,2,3,4,5,6] because of the race condition, but the actual ordering of the execution of the lines of code I had wrong.


I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.


This article is related to


tutorial,web dev,async,javacript,ordering