@adviser/cement - v0.0.0
    Preparing search index...

    Function consumeIterator

    • Consumes an iterator, applying a callback to each item and collecting results.

      Processes sync or async iterators, calling the callback for each item and returning an array of all results. Supports chunked processing with configurable chunk size to yield control back to the event loop periodically.

      Type Parameters

      • T

        Iterator element type

      • R

        Callback return type

      Parameters

      • iter: IterableIterator<T, any, any> | AsyncIterableIterator<T, any, any>

        Iterator or async iterator to consume

      • cb: (msg: T) => R

        Callback function applied to each item

      • params: Partial<StepCfg<R[]>> = {}

        Optional configuration (chunkSize, setTimeoutFn, ctx)

      Returns Promise<R[]>

      Promise resolving to array of all callback results

      function* numbers() {
      yield 1; yield 2; yield 3;
      }

      const results = await consumeIterator(numbers(), (n) => n * 2);
      console.log(results); // [2, 4, 6]

      // Async iterator with custom chunk size
      const data = await consumeIterator(asyncIterator, processItem, {
      chunkSize: 32 // Yield to event loop every 32 items
      });