Gathering detailed insights and metrics for p-map-compat
Gathering detailed insights and metrics for p-map-compat
Gathering detailed insights and metrics for p-map-compat
Gathering detailed insights and metrics for p-map-compat
npm install p-map-compat
Typescript
Module System
Min. Node Version
Node Version
NPM Version
72.4
Supply Chain
98.9
Quality
75.9
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
353
Last Day
2
Last Week
7
Last Month
13
Last Year
353
2 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
0.0.1
Package Id
p-map-compat@0.0.1
Unpacked Size
29.19 kB
Size
5.56 kB
File Count
5
NPM Version
9.7.2
Node Version
20.4.0
Publised On
26 Jan 2024
Cumulative downloads
Total Downloads
Last day
0%
2
Compared to previous day
Last week
0%
7
Compared to previous week
Last month
-45.8%
13
Compared to previous month
Last year
0%
353
Compared to previous year
Compatible version of p-map. Exports cjs and mjs versions of p-map and lets node select the appropriate one. Please see original readme below for details.
npm i --save p-map-compat
1import pMap from 'p-map-compact'; 2import got from 'got'; 3 4const sites = [ 5 getWebsiteFromUsername('sindresorhus'), //=> Promise 6 'https://avajs.dev', 7 'https://github.com' 8]; 9 10const mapper = async site => { 11 const {requestUrl} = await got.head(site); 12 return requestUrl; 13}; 14 15const result = await pMap(sites, mapper, {concurrency: 2}); 16 17console.log(result); 18//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/']
p-map-compact
instead of p-map
. Thats the only change requiredMap over promises concurrently
Useful when you need to run promise-returning & async functions multiple times with different inputs 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.
1npm install p-map
1import pMap from 'p-map'; 2import got from 'got'; 3 4const sites = [ 5 getWebsiteFromUsername('sindresorhus'), //=> Promise 6 'https://avajs.dev', 7 'https://github.com' 8]; 9 10const mapper = async site => { 11 const {requestUrl} = await got.head(site); 12 return requestUrl; 13}; 14 15const result = await pMap(sites, mapper, {concurrency: 2}); 16 17console.log(result); 18//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/']
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.
Returns an async iterable that streams each return value from mapper
in order.
1import {pMapIterable} from 'p-map'; 2 3// Multiple posts are fetched concurrently, with limited concurrency and backpressure 4for await (const post of pMapIterable(postIds, getPostMetadata, {concurrency: 8})) { 5 console.log(post); 6};
Type: AsyncIterable<Promise<unknown> | unknown> | Iterable<Promise<unknown> | unknown>
Synchronous or asynchronous iterable that is iterated over concurrently, calling the mapper
function for each element. Each iterated item is await
'd before the mapper
is invoked so the iterable may return a Promise
that resolves to an item.
Asynchronous iterables (different from synchronous iterables that return Promise
that resolves to an item) can be used when the next item may not be ready without waiting for an asynchronous process to complete and/or the end of the iterable may be reached after the asynchronous process completes. For example, reading from a remote queue when the queue has reached empty, or reading lines from a stream.
Type: Function
Expected to return a Promise
or value.
Type: object
Type: number
(Integer)
Default: Infinity
Minimum: 1
Number of concurrently pending promises returned by mapper
.
Only for pMapIterable
Type: number
(Integer)
Default: options.concurrency
Minimum: options.concurrency
Maximum number of promises returned by mapper
that have resolved but not yet collected by the consumer of the async iterable. Calls to mapper
will be limited so that there is never too much backpressure.
Useful whenever you are consuming the iterable slower than what the mapper function can produce concurrently. For example, to avoid making an overwhelming number of HTTP requests if you are saving each of the results to a database.
Only for pMap
Type: boolean
Default: true
When true
, the first mapper rejection will be rejected back to the consumer.
When false
, instead of stopping when a promise rejects, it will wait for all the promises to settle and then reject with an AggregateError
containing all the errors from the rejected promises.
Caveat: When true
, any already-started async mappers will continue to run until they resolve or reject. In the case of infinite concurrency with sync iterables, all mappers are invoked on startup and will continue after the first rejection. Issue #51 can be implemented for abort control.
Only for pMap
Type: AbortSignal
You can abort the promises using AbortController
.
1import pMap from 'p-map'; 2import delay from 'delay'; 3 4const abortController = new AbortController(); 5 6setTimeout(() => { 7 abortController.abort(); 8}, 500); 9 10const mapper = async value => value; 11 12await pMap([delay(1000), delay(1000)], mapper, {signal: abortController.signal}); 13// Throws AbortError (DOMException) after 500 ms.
Return this value from a mapper
function to skip including the value in the returned array.
1import pMap, {pMapSkip} from 'p-map'; 2import got from 'got'; 3 4const sites = [ 5 getWebsiteFromUsername('sindresorhus'), //=> Promise 6 'https://avajs.dev', 7 'https://example.invalid', 8 'https://github.com' 9]; 10 11const mapper = async site => { 12 try { 13 const {requestUrl} = await got.head(site); 14 return requestUrl; 15 } catch { 16 return pMapSkip; 17 } 18}; 19 20const result = await pMap(sites, mapper, {concurrency: 2}); 21 22console.log(result); 23//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/']
No vulnerabilities found.
No security vulnerabilities found.