Race conditions in JavaScript?

Can JavaScript applications run into race conditions? JavaScript is single threaded, so, nope. Easy, right? And wrong 😛

JavaScript may be a strictly single threaded environment, but the actual program flow consists of asynchronous thunks that are scheduled on an event loop. This is similar to cooperative multitasking environments, and race conditions are a common issue.

async-mutex is a spin off from 6502.ts that I created to synchronize operations that involve multiple communications with a web worker. This has provided so useful that I have factored it out into a separate JavaScript library.

The library provides you with Mutex and Semaphore objects that allow you to serialize operations that span multiple thunks on the event loop.