Re:V8 adds support for top-level await
by olalonde on Wednesday September 25, 2019 @02:26AM
(#21068228)
In that case, the code would be sync and there wouldn't be a "race condition".
[ Parent
| on HN ]
Re:V8 adds support for top-level await
by vidarh on Wednesday September 25, 2019 @03:45AM
(#21068670)
I 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.
Re:V8 adds support for top-level await
by olalonde on Wednesday September 25, 2019 @06:57AM
(#21069490)
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.).