4 ms·
You're reading too much into the example. Your 'database' can be your frontend js state. There is nothing in the example that limits the problem to backend deve
by vidarh 7y ago
You're reading too much into the example. Your 'database' can be your frontend js state. There is nothing in the example that limits the problem to backend development.
- jeswin 7y agoYeah but the impact of race conditions on front-end code (which is always single user) is minimal; unlike the backend where it's catastrophic and not fixable by refreshing the page.
- vidarh 7y agoIt is fixable by refreshing the page if there are no race conditions in the front-end. If anything getting this right on the front-end is often extra problematic because of the high cost of round-tripping state to the server.
- olalonde 7y agoIn that case, the code would be sync and there wouldn't be a "race condition".
- vidarh 7y agoI just the other day had to step in to deal with a race condition in a frontend caused by improperly handling async API requests. It's dangerous to assume that just because JS has a single main thread that you don't need to think about sequencing of operations and locking.
- olalonde 7y agoWhile writing async code can indeed be challenging, I just disagree with the idea that JavaScript is lacking good "locking primitives": > All this async code without decent locking primitives is leading to a rabbit hole of race conditions... No, it's not. Here's a lock: let lock = Promise.resolve() // ... lock = lock.then(() => { /* critical section */ }) For the rare instances where more complex async flow control is required, there are a bunch of libraries for that (e.g. async[0], p-queue[1], etc.). [0] https://caolan.github.io/async/v3/docs.html https://caolan.github.io/async/v3/docs.html [1] https://github.com/sindresorhus/p-queue https://github.com/sindresorhus/p-queue