-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54b51f9
commit dcdbc7a
Showing
7 changed files
with
82 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,61 @@ | ||
declare namespace pMap { | ||
interface Options { | ||
/** | ||
Number of concurrently pending promises returned by `mapper`. | ||
Must be an integer from 1 and up or `Infinity`. | ||
@default Infinity | ||
*/ | ||
readonly concurrency?: number; | ||
export interface Options { | ||
/** | ||
Number of concurrently pending promises returned by `mapper`. | ||
/** | ||
When set to `false`, instead of stopping when a promise rejects, it will wait for all the promises to settle and then reject with an [aggregated error](https://github.com/sindresorhus/aggregate-error) containing all the errors from the rejected promises. | ||
Must be an integer from 1 and up or `Infinity`. | ||
@default true | ||
*/ | ||
readonly stopOnError?: boolean; | ||
} | ||
@default Infinity | ||
*/ | ||
readonly concurrency?: number; | ||
|
||
/** | ||
Function which is called for every item in `input`. Expected to return a `Promise` or value. | ||
When set to `false`, instead of stopping when a promise rejects, it will wait for all the promises to settle and then reject with an [aggregated error](https://github.com/sindresorhus/aggregate-error) containing all the errors from the rejected promises. | ||
@param element - Iterated element. | ||
@param index - Index of the element in the source array. | ||
@default true | ||
*/ | ||
type Mapper<Element = any, NewElement = unknown> = ( | ||
element: Element, | ||
index: number | ||
) => NewElement | Promise<NewElement>; | ||
readonly stopOnError?: boolean; | ||
} | ||
|
||
/** | ||
Function which is called for every item in `input`. Expected to return a `Promise` or value. | ||
@param element - Iterated element. | ||
@param index - Index of the element in the source array. | ||
*/ | ||
export type Mapper<Element = any, NewElement = unknown> = ( | ||
element: Element, | ||
index: number | ||
) => NewElement | Promise<NewElement>; | ||
|
||
/** | ||
@param input - 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. | ||
@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. | ||
@example | ||
``` | ||
import pMap = require('p-map'); | ||
import got = require('got'); | ||
import pMap from 'p-map'; | ||
import got from 'got'; | ||
const sites = [ | ||
getWebsiteFromUsername('https://sindresorhus'), //=> Promise | ||
'https://avajs.dev', | ||
'https://github.com' | ||
]; | ||
(async () => { | ||
const mapper = async site => { | ||
const {requestUrl} = await got.head(site); | ||
return requestUrl; | ||
}; | ||
const mapper = async site => { | ||
const {requestUrl} = await got.head(site); | ||
return requestUrl; | ||
}; | ||
const result = await pMap(sites, mapper, {concurrency: 2}); | ||
const result = await pMap(sites, mapper, {concurrency: 2}); | ||
console.log(result); | ||
//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/'] | ||
})(); | ||
console.log(result); | ||
//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/'] | ||
``` | ||
*/ | ||
declare function pMap<Element, NewElement>( | ||
export default function pMap<Element, NewElement>( | ||
input: Iterable<Element>, | ||
mapper: pMap.Mapper<Element, NewElement>, | ||
options?: pMap.Options | ||
mapper: Mapper<Element, NewElement>, | ||
options?: Options | ||
): Promise<NewElement[]>; | ||
|
||
export = pMap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,10 @@ | |
"email": "[email protected]", | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=12" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd" | ||
|
@@ -39,15 +41,15 @@ | |
"bluebird" | ||
], | ||
"dependencies": { | ||
"aggregate-error": "^3.0.0" | ||
"aggregate-error": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^2.2.0", | ||
"delay": "^4.1.0", | ||
"in-range": "^2.0.0", | ||
"random-int": "^2.0.0", | ||
"time-span": "^3.1.0", | ||
"tsd": "^0.7.4", | ||
"xo": "^0.27.2" | ||
"ava": "^3.15.0", | ||
"delay": "^5.0.0", | ||
"in-range": "^3.0.0", | ||
"random-int": "^3.0.0", | ||
"time-span": "^5.0.0", | ||
"tsd": "^0.14.0", | ||
"xo": "^0.38.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters