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.

71 lines
1.9 KiB

4 years ago
  1. declare namespace pMap {
  2. interface Options {
  3. /**
  4. Number of concurrently pending promises returned by `mapper`.
  5. @default Infinity
  6. */
  7. concurrency?: number;
  8. }
  9. /**
  10. Function which is called for every item in `input`. Expected to return a `Promise` or value.
  11. @param input - Iterated element.
  12. @param index - Index of the element in the source array.
  13. */
  14. type Mapper<Element = any, NewElement = any> = (
  15. input: Element,
  16. index: number
  17. ) => NewElement | Promise<NewElement>;
  18. }
  19. declare const pMap: {
  20. /**
  21. 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.
  22. @param input - Iterated over concurrently in the `mapper` function.
  23. @param mapper - Function which is called for every item in `input`. Expected to return a `Promise` or value.
  24. @example
  25. ```
  26. import pMap = require('p-map');
  27. import got = require('got');
  28. const sites = [
  29. getWebsiteFromUsername('sindresorhus'), //=> Promise
  30. 'ava.li',
  31. 'todomvc.com',
  32. 'github.com'
  33. ];
  34. (async () => {
  35. const mapper = async site => {
  36. const {requestUrl} = await got.head(site);
  37. return requestUrl;
  38. };
  39. const result = await pMap(sites, mapper, {concurrency: 2});
  40. console.log(result);
  41. //=> ['http://sindresorhus.com/', 'http://ava.li/', 'http://todomvc.com/', 'http://github.com/']
  42. })();
  43. ```
  44. */
  45. <Element, NewElement>(
  46. input: Iterable<Element>,
  47. mapper: pMap.Mapper<Element, NewElement>,
  48. options?: pMap.Options
  49. ): Promise<NewElement[]>;
  50. // TODO: Remove this for the next major release, refactor the whole definition to:
  51. // declare function pMap<Element, NewElement>(
  52. // input: Iterable<Element>,
  53. // mapper: pMap.Mapper<Element, NewElement>,
  54. // options?: pMap.Options
  55. // ): Promise<NewElement[]>;
  56. // export = pMap;
  57. default: typeof pMap;
  58. };
  59. export = pMap;