p-map
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The "p-map" NPM package is designed for mapping over promises concurrently. It is particularly useful when you have multiple promise-returning or async functions that need to run simultaneously with different inputs. This package offers a different approach compared to Promise.all()
, as it allows you to not only control concurrency, but also decide whether or not to stop iteration when there's an error.
To use the p-map package, you first need to install it using npm with the command npm install p-map
. Once installed, you can import it into your JavaScript file. Here's an example of how to use it:
import pMap from 'p-map';
import got from 'got';
const sites = [
getWebsiteFromUsername('sindresorhus'), //=> Promise
'https://avajs.dev',
'https://github.com'
];
const mapper = async site => {
const {requestUrl} = await got.head(site);
return requestUrl;
};
const result = await pMap(sites, mapper, {concurrency: 2});
console.log(result);
//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/']
In the above example, p-map
is used to concurrently send GET requests to an array of websites and waits until they all respond before printing out a resulting array of the URLs.
The documentation for "p-map" can be found within the README.md file of its GitHub repository, available at https://github.com/sindresorhus/p-map. The documentation includes an overview of the package's functions and how to use it, along with its API specifications, options, and examples of how to integrate it into your code.