4 ms·
While writing async code can indeed be challenging, I just disagree with the idea that JavaScript is lacking good "locking primitives": > All this async code w
by olalonde 7y ago
While 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