|
|
|
|
|
by littlestymaar
2425 days ago
|
|
What would you do instead? Use a green thread system on top of a single-threaded VM? Great, now you have two kinds of blocking calls: the one which only block its own thread, and the one which block all threads at the same time because it blocks the VM itself. How ergonomic! Remember, the single threaded character of the js VM is not an implementation detail, it's part of the spec. Hate it or love it but the web works this way. Also, if you think threads are a good concurrency abstraction, let's play a little game: consider you need to read 1M files on a spinning disk. How many threads do you need to run on to get the maximum read performance: a) one per file b) just one c) one per core d) a magic number which depends on your hard disk's firmware and your workload. Convenient and intuitive right? Threads are a dated concurrency primitive which would have died long ago if wasn't also a good parallelism primitive. |
|
You could only have the first kind, but it's the same situation with async/await. Only async/await tracks the "good" kind of blocking, yet lets the "bad" kind go untracked.
> How many threads do you need to run on yo get the maximum read performance
The exact same number as you would for doing it with `await Promise.all` The same knowledge you have about the scheduling mechanism doesn't go away if you're no longer required to annotate functions with `async`.
> Threads are a dated concurrency primitive which would have died long ago if wasn't also a good parallelism primitive.
Maybe they are, but async/await are the exact same construct only that you have to annotate every blocking function with "async" and every blocking call with "await". If you had a language with threads but no async/await that had that requirement you would not have been able to tell the difference between it and one that has async/await.