proposal-explicit-resource-management icon indicating copy to clipboard operation
proposal-explicit-resource-management copied to clipboard

implement features required for structured concurrency

Open graingert opened this issue 1 year ago • 1 comments

I'd like to be able to implement a timeout or TaskGroup context manager like in Trio/asyncio:

import sleep from 'effection';

function* example () {
    using* tg = new TaskGroup();
    tg.startSoon(sleep, 10);
    yield* sleep(1);
}

This requires access to the error: https://github.com/tc39/proposal-explicit-resource-management/issues/49

and support for generator functions in disposing, specifically a Symbol.generatorDispose that supports a generator function that gets called when using using*

graingert avatar Oct 11 '24 07:10 graingert

@rbuckton or anyone else with deeper insight into this proposal - has there been any consideration for supporting using in generator functions in the same way as for async functions? It seems odd to make generator functions second class citizens in a way unless there is a good reason? Are there complications in specifying an analogue [Symbol.generatorDispose] interface?

This seems like it would be a very useful feature for those of us who frequently use generators, though I couldn't say for sure if this would be more complicated than for async functions. At a surface level of thinking it seems like it would make sense though. As for a simple motivation consider a program that yields strings for logging (though much more interesting examples can of course be produced).

function* foo() {
  yield 'Acquiring resource'
  using* x = getResource();
}

const getResourece = () => {
  [Symbol.generatorDispose]*() {
     yield 'Disposing!'
     // ... dispose
  }
}

of course this then opens the door to async generators :) As far as I'm concerned having the interface for generators would be sufficient as you can always yield a promise co-style and handle it yourself.

philipnilsson avatar Feb 20 '25 15:02 philipnilsson

@graingert You should check out https://github.com/thefrontside/effection which has a very robust implementation of structured concurrency very close to what you're describing.

cowboyd avatar Apr 25 '25 20:04 cowboyd