crnt - v0.0.31
    Preparing search index...

    Function toBufferedAsyncIterable

    • Creates a buffered AsyncIterable that pre-loads N elements from the source iterable.

      This function provides lookahead buffering, which can be useful for:

      • Smoothing out irregular data arrival patterns
      • Providing better throughput for downstream processing
      • Memory-controlled prefetching from slow sources

      The buffer is filled asynchronously and items are yielded as soon as they're available. When the buffer is full, it waits for items to be consumed before loading more.

      Type Parameters

      • T

        The type of items in the iterable

      Parameters

      • source: AsyncIterable<T>

        The source AsyncIterable to buffer

      • bufferSize: number = 10

        Maximum number of items to buffer ahead (default: 10)

      Returns AsyncIterable<T>

      A new AsyncIterable with buffered access to the source

      async function* slowSource() {
      for (let i = 0; i < 100; i++) {
      await delay(100); // Simulate slow data arrival
      yield i;
      }
      }

      // Buffer 20 items ahead for smoother processing
      const buffered = toBufferedAsyncIterable(slowSource(), 20);

      for await (const item of buffered) {
      // Process items with reduced waiting time
      console.log(item);
      }