This is a simple example of how to get a concurrency issue in JavaScript while using async/await.
In the initial example, async-concurreny-issue.js
, the main function starts two tasks that were supposed to run asynchronously. The big problem here is that runTask()
has side effects and changes an internal state represented by currentTaskId
. After going through a function call that requires awaiting on a promise (promiseMe()
), the task expects currentTaskId
to still have the same id assigned to it a couple of lines above. Even if promiseMe()
does actually nothing, it will still execute asynchronously. That should be no problem because we are await
ing on it in runTask()
, right? Yeah, but the problem is that main()
is not doing its job and await
is not being used there. This means that main()
fires runTask(2)
immediately after calling runTask(1)
, so it runs before the call to promiseMe()
has the chance to return - it can only return in the next event loop tick, since it is behind a p