concurrentMap()
concurrentMap<
Input,Output,ErrorOutput>(options,mapper,errorMapper?):ConcurrentMap<Input,Output,ErrorOutput>
Defined in: src/ConcurrentMap.ts:204
Creates an operator like map that runs up to options.concurrency
mapper calls at the same time.
Results are emitted in the order the calls complete, which is not necessarily the order of the input.
If mapper throws and an errorMapper is given, its return value is
emitted instead.
The operator starts working when the iteration starts, not when it is
applied. If the source throws, or mapper throws and no errorMapper is
given (or errorMapper throws), the iteration ends with that error: no new
calls are started and the results of calls still running are discarded.
Type Parameters
Input
Input
Output
Output
ErrorOutput
ErrorOutput = never
Parameters
options
see ConcurrentMapOptions
mapper
(req) => Output | Promise<Output>
transforms an item; may be asynchronous
errorMapper?
(req, error) => ErrorOutput | Promise<ErrorOutput>
turns an error thrown by mapper into a value
Returns
ConcurrentMap<Input, Output, ErrorOutput>
an operator object
Example
import { setTimeout as sleep } from "node:timers/promises";
const delays = [60, 10, 30];
const work = async (ms: number) => {
await sleep(ms);
return ms;
};
await chain(delays).concurrentMap({ concurrency: 3 }, work).toArray();
// => [10, 30, 60]
await chain(delays).concurrentMap({ concurrency: 1 }, work).toArray();
// => [60, 10, 30]