@metreeca/flow - v0.9.21
    Preparing search index...

    Function batch

    • Creates a task collecting consecutive items into fixed-size batches.

      Batches are emitted as soon as they fill up, in source order and with items in source order, so only one batch is held in memory at a time; the last batch is emitted short if the feed ends before it fills up, and no batch is emitted at all for an empty feed.

      Warning

      An unbounded size accumulates the whole feed in memory before emitting the single batch holding it. For large or infinite feeds, this may exhaust memory or never complete: batch by a positive size to keep consumption bounded.

      Type Parameters

      • V

        The type of items in the feed

      Parameters

      • size: number = 0

        The maximum number of items per batch, defaulting to 0; values less than 1 are treated as unbounded, collecting the whole feed into a single batch

      Returns Task<V, readonly V[]>

      A task yielding the read-only lists of the items collected into each batch

      If size is not an integer

      await pipe(
      (items(1, 2, 3, 4, 5))
      (batch(2))
      (toArray())
      ); // [[1, 2], [3, 4], [5]]

      await pipe(
      (items(1, 2, 3))
      (batch())
      (toArray())
      ); // [[1, 2, 3]]