Comparing version 5.0.0 to 5.1.0
@@ -41,3 +41,3 @@ export interface Options { | ||
const sites = [ | ||
getWebsiteFromUsername('https://sindresorhus'), //=> Promise | ||
getWebsiteFromUsername('sindresorhus'), //=> Promise | ||
'https://avajs.dev', | ||
@@ -62,2 +62,34 @@ 'https://github.com' | ||
options?: Options | ||
): Promise<NewElement[]>; | ||
): Promise<Array<Exclude<NewElement, typeof pMapSkip>>>; | ||
/** | ||
Return this value from a `mapper` function to skip including the value in the returned array. | ||
@example | ||
``` | ||
import pMap, {pMapSkip} from 'p-map'; | ||
import got from 'got'; | ||
const sites = [ | ||
getWebsiteFromUsername('sindresorhus'), //=> Promise | ||
'https://avajs.dev', | ||
'https://example.invalid', | ||
'https://github.com' | ||
]; | ||
const mapper = async site => { | ||
try { | ||
const {requestUrl} = await got.head(site); | ||
return requestUrl; | ||
} catch { | ||
return pMapSkip | ||
} | ||
}; | ||
const result = await pMap(sites, mapper, {concurrency: 2}); | ||
console.log(result); | ||
//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/'] | ||
``` | ||
*/ | ||
export const pMapSkip: unique symbol; |
20
index.js
@@ -22,2 +22,3 @@ import AggregateError from 'aggregate-error'; | ||
const errors = []; | ||
const skippedIndexes = []; | ||
const iterator = iterable[Symbol.iterator](); | ||
@@ -45,2 +46,6 @@ let isRejected = false; | ||
} else { | ||
for (const skippedIndex of skippedIndexes) { | ||
result.splice(skippedIndex, 1); | ||
} | ||
resolve(result); | ||
@@ -58,3 +63,14 @@ } | ||
const element = await nextItem.value; | ||
result[index] = await mapper(element, index); | ||
if (isRejected) { | ||
return; | ||
} | ||
const value = await mapper(element, index); | ||
if (value === pMapSkip) { | ||
skippedIndexes.push(index); | ||
} else { | ||
result[index] = value; | ||
} | ||
resolvingCount--; | ||
@@ -84,1 +100,3 @@ next(); | ||
} | ||
export const pMapSkip = Symbol('skip'); |
{ | ||
"name": "p-map", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "Map over promises concurrently", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -22,3 +22,3 @@ # p-map | ||
const sites = [ | ||
getWebsiteFromUsername('https://sindresorhus'), //=> Promise | ||
getWebsiteFromUsername('sindresorhus'), //=> Promise | ||
'https://avajs.dev', | ||
@@ -76,2 +76,32 @@ 'https://github.com' | ||
### pMapSkip | ||
Return this value from a `mapper` function to skip including the value in the returned array. | ||
```js | ||
import pMap, {pMapSkip} from 'p-map'; | ||
import got from 'got'; | ||
const sites = [ | ||
getWebsiteFromUsername('sindresorhus'), //=> Promise | ||
'https://avajs.dev', | ||
'https://example.invalid', | ||
'https://github.com' | ||
]; | ||
const mapper = async site => { | ||
try { | ||
const {requestUrl} = await got.head(site); | ||
return requestUrl; | ||
} catch { | ||
return pMapSkip | ||
} | ||
}; | ||
const result = await pMap(sites, mapper, {concurrency: 2}); | ||
console.log(result); | ||
//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/'] | ||
``` | ||
## p-map for enterprise | ||
@@ -78,0 +108,0 @@ |
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
10319
156
120