@metreeca/flow - v0.10.0
    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; 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

      • Incremental: batches are emitted as they fill up where size is positive, so the reported feed runs dry as the feed drawn from does; an unbounded size drains the whole feed before emitting the single batch holding it, so an infinite feed never completes.
      • Materialising: an unbounded size holds the whole feed in memory, so a large feed may exhaust it; batch by a positive size to keep consumption bounded.
      • Stateful: items are collected across draws, so a task invoked per nested feed or per run batches each independently, closing every one with a short batch rather than filling it from the items that follow.

      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

      TypeError 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]]