3 ms·
Do you really want to block all of your http calls on the result of the prior call in the off-chance that they might share a result just so you can only cache t
by Arelius 7y ago
Do you really want to block all of your http calls on the result of the prior call in the off-chance that they might share a result just so you can only cache the results and not the promises?
The reason we don't have a mutex in javascript is that there are better solutions to the problems it solves.
- hombre_fatal 7y agoMaybe 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.
- inglor 7y agoA 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.