Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Created February 4, 2026 15:27
Show Gist options
  • Select an option

  • Save cowboyd/5c17ce196979a2ce925a35e10c5db517 to your computer and use it in GitHub Desktop.

Select an option

Save cowboyd/5c17ce196979a2ce925a35e10c5db517 to your computer and use it in GitHub Desktop.
Consuming Explicitly Managed Resources
import { main, type Operation, resource, until } from "@effection/effection";
class Thing {
constructor(public num: number) {}
async [Symbol.asyncDispose]() {
console.log(`[${this.num}]`, 'Adieu!');
}
}
export function using<T extends AsyncDisposable>(value: T): Operation<T> {
return resource(function*(provide) {
try {
yield* provide(value);
} finally {
yield* until(value[Symbol.asyncDispose].call(value) as Promise<void>);
}
})
}
await main(function*() {
let one = yield* using(new Thing(1));
let two = yield* using(new Thing(2));
console.log({ one, two });
});
//=> { one: Thing { num: 1 }, two: Thing { num: 2 } }
//=> [2] Adieu!
//=> [1] Adieu!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment