Re:V8 adds support for top-level await
by hombre_fatal on Tuesday September 24, 2019 @11:03PM
(#21067196)
Maybe I don't understand your response, but a map of promises to handle parallel in-flight requests for the same resource (which the grandparent pitched) is basically the most elegant yet simple solution to this common problem.
I don't really see how it's a mutex though, you're just returning the same promise to multiple requests. It's far simpler and doesn't use a locking construct.
Re:V8 adds support for top-level await
by inglor on Wednesday September 25, 2019 @02:47AM
(#21068349)
A promise is a lock in that resolving it is the 'release' and awaiting it is `acquire`.
For example:
``` let release; const acquire = new Promise(resolve => release = resolve);
(async () => {
await acquire;
// use here
release(); // now others can use it
})();
```This is a bit different because multiple people can await the lock here so it's like a "one time multicast mutex" making it more akin to condition variables.
That said - thinking about it as a mutex is a really backwards way in my opinion.
The reason we don't have a mutex in javascript is that there are better solutions to the problems it solves.