@jiminp/tooltool
    Preparing search index...

    Function createAsyncChannel

    • Creates an async channel that buffers values and replays them to each iterator.

      Type Parameters

      • Y

        Yielded value type.

      • R = void

        Return value type.

      • T = unknown

        Thrown error type (default: unknown).

      Returns AsyncChannel<Y, R, T>

      An AsyncChannel.

      Each call to [Symbol.asyncIterator]() returns a fresh iterator starting from the first buffered value. Calling result() multiple times returns the same promise without affecting iteration. A rejected result is internally observed, so iteration-only consumers do not cause an unhandled result rejection; callers of result() still receive it.

      Subscribers run synchronously. If one throws, it is detached, all other pending consumers are notified, and the first callback exception is rethrown to the producer. Callback exceptions do not change the channel's result, including on completion or error. Exceptions during buffered replay instead propagate from the subscription call.

      Buffered events are retained for the lifetime of the channel; memory grows with the number of events. Stopping one iterator does not stop the producer.

      const ch = createAsyncChannel<number, string>();
      ch.next(1);
      ch.next(2);
      ch.complete("done");

      for await (const v of ch) console.log(v); // 1, 2
      console.log(await ch.result()); // "done"