web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.0 KiB

  1. # p-map [![Build Status](https://travis-ci.org/sindresorhus/p-map.svg?branch=master)](https://travis-ci.org/sindresorhus/p-map)
  2. > Map over promises concurrently
  3. Useful when you need to run promise-returning & async functions multiple times with different inputs concurrently.
  4. ## Install
  5. ```
  6. $ npm install p-map
  7. ```
  8. ## Usage
  9. ```js
  10. const pMap = require('p-map');
  11. const got = require('got');
  12. const sites = [
  13. getWebsiteFromUsername('https://sindresorhus'), //=> Promise
  14. 'https://ava.li',
  15. 'https://github.com'
  16. ];
  17. (async () => {
  18. const mapper = async site => {
  19. const {requestUrl} = await got.head(site);
  20. return requestUrl;
  21. };
  22. const result = await pMap(sites, mapper, {concurrency: 2});
  23. console.log(result);
  24. //=> ['https://sindresorhus.com/', 'https://ava.li/', 'https://github.com/']
  25. })();
  26. ```
  27. ## API
  28. ### pMap(input, mapper, options?)
  29. 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.
  30. #### input
  31. Type: `Iterable<Promise | unknown>`
  32. Iterated over concurrently in the `mapper` function.
  33. #### mapper(element, index)
  34. Type: `Function`
  35. Expected to return a `Promise` or value.
  36. #### options
  37. Type: `object`
  38. ##### concurrency
  39. Type: `number` (Integer)\
  40. Default: `Infinity`\
  41. Minimum: `1`
  42. Number of concurrently pending promises returned by `mapper`.
  43. ##### stopOnError
  44. Type: `boolean`\
  45. Default: `true`
  46. 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.
  47. ## p-map for enterprise
  48. Available as part of the Tidelift Subscription.
  49. The maintainers of p-map and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-p-map?utm_source=npm-p-map&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
  50. ## Related
  51. - [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency
  52. - [p-filter](https://github.com/sindresorhus/p-filter) - Filter promises concurrently
  53. - [p-times](https://github.com/sindresorhus/p-times) - Run promise-returning & async functions a specific number of times concurrently
  54. - [p-props](https://github.com/sindresorhus/p-props) - Like `Promise.all()` but for `Map` and `Object`
  55. - [p-map-series](https://github.com/sindresorhus/p-map-series) - Map over promises serially
  56. - [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control
  57. - [More…](https://github.com/sindresorhus/promise-fun)