The type of values in the async iterable
Optionalcallback: (value: T, index: number, worker: number) => unknown
Optional function to apply to each value
Configuration options
Optionalconcurrency?: numberNumber of concurrent worker threads (default: 1)
Optionalsignal?: AbortSignalAborting stops pulling values, returns the source iterator, waits for
callbacks already in flight and rejects with signal.reason
A consumer function that returns a promise resolving to void
This function creates a terminal consumer that processes every value in an async iterable, typically for side effects. It doesn't produce a value but simply ensures that all values are processed by the optional callback function.
The consumer supports concurrent processing with the concurrency option. When greater than 1,
multiple worker threads will process values in parallel, which can improve performance for
CPU-bound or I/O-bound operations.
If no callback is provided, this effectively just drains the async iterable, ensuring all values are processed.
// Simple sequential processing
await G.pipe(
G.ofIterable([1, 2, 3, 4, 5]),
G.consume(value => {
console.log(`Processing: ${value}`);
})
);
// Concurrent processing of CPU-intensive operations
await G.pipe(
G.ofIterable(largeDataset),
G.consume(async item => {
await processItemIntensively(item);
}, { concurrency: 4 }) // Use 4 concurrent workers
);
// Just drain the iterable (run it to completion)
await G.pipe(
generatorThatHasSideEffects(),
G.consume() // No callback, just ensures all values are processed
);
// Concurrent processing with database operations
await G.pipe(
G.ofIterable(records),
G.consume(async record => {
await db.insert(record);
}, { concurrency: 10 }) // Handle 10 DB operations concurrently
);
Creates a consumer that processes all values from an async iterable without returning a result.