Adding structured concurrency to JavaScript

11 points by bakkot


manuel

Related: https://lobste.rs/s/fedhvm/await_event_horizon_javascript

jonathannen

I think I was getting hung up on controller.wrap(promise) alternative, and had to mentally rewrite it in terms of allSettled to get my head around it.

That helped me see the distinction a bit better: for the "known batch of promises" case, allSettled gets surprisingly close, but the broader thing being aimed at here is scope-owned shutdown rather than just result aggregation (if I read that correctly)?

Also, I learned await using existed, so this was educational on multiple fronts.

Johz

This is a cool set of ideas. I agree that there needs to be a better way of combining using and abort signals, and just generally tighter integration of Disposable with existing browser APIs.

I've tried in my own projects an approach where I've mostly avoided abort signals and concentrated on using Disposable/AsyncDisposable everywhere, but ultimately this fails as soon as you hit promises. Because promises don't have any native cancellation system, if you want to dispose of a promise, you need some sort of out-of-band cancellation mechanism, and while you can put something together with disposable wrappers, ultimately you're just reinventing abort signals. So any system that links abort signals and disposables seems sensible to me.

I think concentrating on the Promise.map/.race methods makes a lot of sense. In my experience, these are currently difficult functions to use because of the numerous cancellation hazards that can occur, especially .race because the whole point of that function is that most of the promises passed in should not run to completion, and yet the naive approach means that they all will. I agree that adding Promise.raceButWithABetterAPI probably isn't the best approach, but I think thinking about what the average .race call would look like is a good test of whether new APIs are ergonomic to use.