@jiminp/tooltool
    Preparing search index...

    Function toAsyncGenerator

    • 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.

      Type Parameters

      • Y

        The type of yielded values

      • R = void

        The type of the return value

      • T = unknown

        The type of thrown values/errors (default: unknown)

      Parameters

      Returns AsyncGenerator<Y, R>

      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);
      }
      // Simple counter example
      const counter = toAsyncGenerator<number, string>((callbacks) => {
      let count = 0;
      const interval = setInterval(() => {
      callbacks.yeet(count++);
      if (count >= 5) {
      clearInterval(interval);
      callbacks.done("finished");
      }
      }, 100);
      });