proposal-explicit-resource-management
proposal-explicit-resource-management copied to clipboard
implement features required for structured concurrency
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*
@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.
@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.