Comparing version 4.0.0 to 5.0.0
@@ -1,33 +0,31 @@ | ||
declare namespace pMap { | ||
interface Options { | ||
/** | ||
Number of concurrently pending promises returned by `mapper`. | ||
export interface Options { | ||
/** | ||
Number of concurrently pending promises returned by `mapper`. | ||
Must be an integer from 1 and up or `Infinity`. | ||
Must be an integer from 1 and up or `Infinity`. | ||
@default Infinity | ||
*/ | ||
readonly concurrency?: number; | ||
@default Infinity | ||
*/ | ||
readonly concurrency?: number; | ||
/** | ||
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. | ||
@default true | ||
*/ | ||
readonly stopOnError?: boolean; | ||
} | ||
/** | ||
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. | ||
@@ -39,30 +37,26 @@ @param mapper - Function which is called for every item in `input`. Expected to return a `Promise` or value. | ||
``` | ||
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://ava.li', | ||
'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://ava.li/', '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; |
17
index.js
@@ -1,12 +0,11 @@ | ||
'use strict'; | ||
const AggregateError = require('aggregate-error'); | ||
import AggregateError from 'aggregate-error'; | ||
module.exports = async ( | ||
export default async function pMap( | ||
iterable, | ||
mapper, | ||
{ | ||
concurrency = Infinity, | ||
concurrency = Number.POSITIVE_INFINITY, | ||
stopOnError = true | ||
} = {} | ||
) => { | ||
) { | ||
return new Promise((resolve, reject) => { | ||
@@ -17,3 +16,3 @@ if (typeof mapper !== 'function') { | ||
if (!((Number.isSafeInteger(concurrency) || concurrency === Infinity) && concurrency >= 1)) { | ||
if (!((Number.isSafeInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency >= 1)) { | ||
throw new TypeError(`Expected \`concurrency\` to be an integer from 1 and up or \`Infinity\`, got \`${concurrency}\` (${typeof concurrency})`); | ||
@@ -43,3 +42,3 @@ } | ||
if (resolvingCount === 0) { | ||
if (!stopOnError && errors.length !== 0) { | ||
if (!stopOnError && errors.length > 0) { | ||
reject(new AggregateError(errors)); | ||
@@ -75,3 +74,3 @@ } else { | ||
for (let i = 0; i < concurrency; i++) { | ||
for (let index = 0; index < concurrency; index++) { | ||
next(); | ||
@@ -84,2 +83,2 @@ | ||
}); | ||
}; | ||
} |
{ | ||
"name": "p-map", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"description": "Map over promises concurrently", | ||
@@ -13,4 +13,6 @@ "license": "MIT", | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=12" | ||
}, | ||
@@ -43,13 +45,13 @@ "scripts": { | ||
"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" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# p-map [![Build Status](https://travis-ci.org/sindresorhus/p-map.svg?branch=master)](https://travis-ci.org/sindresorhus/p-map) | ||
# p-map | ||
@@ -7,2 +7,4 @@ > Map over promises concurrently | ||
This is different from `Promise.all()` in that you can control the concurrency and also decide whether or not to stop iterating when there's an error. | ||
## Install | ||
@@ -17,22 +19,20 @@ | ||
```js | ||
const pMap = require('p-map'); | ||
const got = require('got'); | ||
import pMap from 'p-map'; | ||
import got from 'got'; | ||
const sites = [ | ||
getWebsiteFromUsername('https://sindresorhus'), //=> Promise | ||
'https://ava.li', | ||
'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://ava.li/', 'https://github.com/'] | ||
})(); | ||
console.log(result); | ||
//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/'] | ||
``` | ||
@@ -64,3 +64,3 @@ | ||
Type: `number` (Integer)\ | ||
Type: `number` *(Integer)*\ | ||
Default: `Infinity`\ | ||
@@ -67,0 +67,0 @@ Minimum: `1` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Yes
8677
117
+ Addedaggregate-error@4.0.1(transitive)
+ Addedclean-stack@4.2.0(transitive)
+ Addedescape-string-regexp@5.0.0(transitive)
+ Addedindent-string@5.0.0(transitive)
- Removedaggregate-error@3.1.0(transitive)
- Removedclean-stack@2.2.0(transitive)
- Removedindent-string@4.0.0(transitive)
Updatedaggregate-error@^4.0.0