@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
61 lines (60 loc) • 2.2 kB
TypeScript
import { ErrorMode } from '../error/errorMode.js';
import type { CommonLogger } from '../log/commonLogger.js';
import { type AbortableAsyncMapper } from '../types.js';
export interface PMapOptions {
/**
* Number of concurrently pending promises returned by `mapper`.
*
* Defaults to Infitity.
*/
concurrency?: number;
/**
* @default ErrorMode.THROW_IMMEDIATELY
*
* When set to THROW_AGGREGATED, instead of stopping when a promise rejects, it will wait for all the promises to settle and then reject with an Aggregated error.
*
* When set to SUPPRESS - will ignore errors and return results from successful operations.
* When in SUPPRESS - errors are still logged via the `logger` (which defaults to console).
*/
errorMode?: ErrorMode;
/**
* Default to `console`.
* Pass `null` to skip logging completely.
*/
logger?: CommonLogger | null;
}
/**
* Forked from https://github.com/sindresorhus/p-map
*
* Improvements:
* - Exported as { pMap }, so IDE auto-completion works
* - Included Typescript typings (no need for @types/p-map)
* - Compatible with pProps (that had typings issues)
* - Preserves async stack traces (in selected cases)
*
* Returns a `Promise` that is fulfilled when all promises in `input` and ones returned from `mapper` are fulfilled,
* or rejects if any of the promises reject. The fulfilled value is an `Array` of the fulfilled values returned
* from `mapper` in `input` order.
*
* @param iterable - Iterated over concurrently in the `mapper` function.
* @param mapper - Function which is called for every item in `input`. Expected to return a `Promise` or value.
* @param opt - Options-object.
*
* @example
*
* const sites = [
* 'ava.li',
* 'todomvc.com
* ];
*
* (async () => {
* const mapper = async site => {
* const {requestUrl} = await got.head(site);
* return requestUrl;
* };
*
* const result = await pMap(sites, mapper, {concurrency: 2});
* //=> ['http://ava.li/', 'http://todomvc.com/']
* })();
*/
export declare function pMap<IN, OUT>(iterable: Iterable<IN>, mapper: AbortableAsyncMapper<IN, OUT>, opt?: PMapOptions): Promise<OUT[]>;