Hacker News new | ask | show | jobs
by eyelidlessness 1109 days ago
> The less interesting version is that you do always have to option to just block. You can call a task runner on that future, and synchronously block until that specific future completes itself. This isn’t the greatest approach, because we probably don’t want to block, but it is an option.

My first thought, coming from a JS perspective, was “no, you actually can’t do that”, because blocking the event loop will prevent resolution of the Promise. My next thought was that this is particular to a single threaded, cooperative concurrency model, so of course “can’t” is situational.

But on further thought, it’s much closer to my first thought: you can do that if you can interleave asynchronous ticks with synchronous ones, but only if you can determine there are no data races (or accept them if there are). This is something I suspect Haskell (or Rust?) is probably well suited for, but for JS it’s a complete non-starter.

Another comment made the point that the async keyword (the explicit “color”) is at issue, and I nearly made this comment there because it raised the same concerns for me from a different angle: if you can “await” from an otherwise synchronous function, you’re either accepting data races, blocking permanently, or implicitly “coloring” all functions as async with the same implication that you can statically eliminate races.

At which point, writing this, I realized this would all be so much simpler if everything was a statically analyzable DAG with transactional semantics.

1 comments

In Rust, blocking the current thread on future completion is spelled `block_on(your_future)`. https://docs.rs/futures/latest/futures/executor/fn.block_on....
Looks like it satisfies the cooperative interleaving via LocalPool. I’m not surprised there’s a built in solution, I’d expect (safe) Rust to have all the information it needs about pertinent dependencies to make cooperative polling feasible and safe. I am a bit surprised it sounds ergonomically exactly like what JS devs might naively assume they could do (I guess what I mean is… I was expecting a little more ceremony?).