batch()
batch<
T>(sizeOrOptions):OperatorFunction<T,T[]>
Defined in: src/Batch.ts:43
Creates an operator that groups items into arrays.
A number is a shortcut for the size option. A batch is emitted when it
reaches size items or, if timeFrame is set, that many milliseconds
after its first item. The remainder at the end of the source is emitted as
a smaller batch.
If the source throws, the batch collected so far is emitted first and the error is thrown after it.
Type Parameters
T
T
Parameters
sizeOrOptions
number | BatchOptions
the batch size or BatchOptions
Returns
OperatorFunction<T, T[]>
an operator function
Example
await chain(range(0, 5)).batch(2).toArray();
// => [[0, 1], [2, 3], [4]]
import { setTimeout as sleep } from "node:timers/promises";
async function* slow() {
yield 1;
await sleep(150);
yield 2;
}
await chain(slow()).batch({ size: 10, timeFrame: 50 }).toArray();
// => [[1], [2]]