The type of yielded values
The type of the return value
The type of thrown values/errors (default: unknown)
A function that receives callbacks
for yielding (yeet), returning (done), and throwing (fail)
An async generator that yields values as they are produced
// Convert a callback-based event emitter to an async generator
const generator = toAsyncGenerator<string, void>((callbacks) => {
eventEmitter.on('data', (data) => callbacks.yeet(data));
eventEmitter.on('end', () => callbacks.done());
eventEmitter.on('error', (err) => callbacks.fail(err));
});
for await (const value of generator) {
console.log(value);
}
Converts a callback-based API to an async generator.
This function bridges imperative, callback-based APIs with async generator patterns. The executor function receives callbacks (yeet, done, fail) that can be called asynchronously to produce values, complete, or throw errors.