News for hackers. Stuff that matters.
Re:V8 adds support for top-level await by gregmac on Tuesday September 24, 2019 @07:47PM (#21065995)
> two sequential lines of code are no longer two truly sequential instructions

I've done a lot of async/await and I really can't think of any situations where this has been a concern for me. If you're mutating state in method calls without explicitly passing it around, that might be an issue but that's a deeper design issue IMHO.

Re:V8 adds support for top-level await by PetahNZ on Tuesday September 24, 2019 @07:54PM (#21066055)
One use case I often find is a caching layer between JS and a HTTP call. If there are 2 calls to a non cached endpoint, the HTTP will fire off twice before caching the result. You can work around this by returning the first promise from the cache, but this is essentially a mutex. Having locking primitives would solve this and require less boilerplate code.
Re:V8 adds support for top-level await by tiglionabbit on Tuesday September 24, 2019 @11:46PM (#21067417)
Caching the promise is a perfectly good solution. Locks aren't needed.
Re:V8 adds support for top-level await by kbenson on Tuesday September 24, 2019 @08:31PM (#21066310)
Seems like you should be able to build a lock mechanism fairly simply with async/await, but I agree, it would be nice if it was a built in primitive. It does feel a little silly to implement a lock function when the engine much be using one to support its async functionality in the first place, so you're introducing a lot of inefficiency.
Re:V8 adds support for top-level await by Arelius on Tuesday September 24, 2019 @10:21PM (#21066952)
I don't think it's true that the engine must be using a mutex to support it's async functionality. Or that it is true that an OS mutex will be any more efficient than alternative solutions.

A mutex really can only exist to serialize async code. If you want it serialized that likely means that code shouldn't be async in the first place.

Re:V8 adds support for top-level await by kbenson on Wednesday September 25, 2019 @01:01AM (#21067744)
You're right that it may not be required for the async functionality (especially since lock-free schedulers exist, and the engine is basically a single processor scheduler for processes). I do think it's likely in use other places though, just because it's much easier to write correct code with one.
Re:V8 adds support for top-level await by matharmin on Wednesday September 25, 2019 @02:53AM (#21068386)
Implementing a mutex is very simple and efficient in JavaScript - probably much lower overhead than the Promises itself.
Re:V8 adds support for top-level await by jnbiche on Tuesday September 24, 2019 @11:17PM (#21067271)
You write this boilerplate to avoid one extra non-cached HTTP call on a (browser?) client? Am I understanding correctly?
Re:V8 adds support for top-level await by londons_explore on Wednesday September 25, 2019 @03:37AM (#21068631)
One extra call could be a multi-megabyte video chunk, or the code flow might mean this case always occurs, potentially hundreds of times before the promise resolves.
Re:V8 adds support for top-level await by matharmin on Wednesday September 25, 2019 @02:56AM (#21068395)
It's really not a lot of boilerplate - once you have the small utility function, it can be one additional line of code.
Re:V8 adds support for top-level await by random314 on Wednesday September 25, 2019 @11:56AM (#21071993)
When your program is single threaded, a simple boolean flag variable can act as a mutex, you don't need a mutex for this.

Mutexes are for situations where flag variables can change state between your instructions to check and set a flag. This can only happen in multithreaded or interrupt driven code.

Re:V8 adds support for top-level await by Arelius on Tuesday September 24, 2019 @10:18PM (#21066936)
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.

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.