index.d.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. declare const pMapSeries: {
  2. /**
  3. Map over promises serially.
  4. @param input - Mapped over serially in the `mapper` function.
  5. @param mapper - Expected to return a value. If it's a `Promise`, it's awaited before continuing with the next iteration.
  6. @returns Fulfills when all promises in `input` and ones returned from `mapper` are fulfilled, or rejects if any of the promises reject.
  7. @example
  8. ```
  9. import pMapSeries = require('p-map-series');
  10. const keywords = [
  11. getTopKeyword() //=> Promise
  12. 'rainbow',
  13. 'pony'
  14. ];
  15. let scores = [];
  16. const mapper = async keyword => {
  17. const score = await fetchScore(keyword);
  18. scores.push(score);
  19. return {keyword, score};
  20. });
  21. (async () => {
  22. console.log(await pMapSeries(keywords, mapper));
  23. // [
  24. // {
  25. // keyword: 'unicorn',
  26. // score: 99
  27. // },
  28. // {
  29. // keyword: 'rainbow',
  30. // score: 70
  31. // },
  32. // {
  33. // keyword: 'pony',
  34. // score: 79
  35. // }
  36. // ]
  37. })();
  38. ```
  39. */
  40. <ValueType, MappedValueType>(
  41. input: Iterable<PromiseLike<ValueType> | ValueType>,
  42. mapper: (
  43. element: ValueType,
  44. index: number
  45. ) => PromiseLike<MappedValueType> | MappedValueType
  46. ): Promise<MappedValueType[]>;
  47. // TODO: Remove this for the next major release, refactor the whole definition to:
  48. // declare function pMapSeries<ValueType, MappedValueType>(
  49. // input: Iterable<PromiseLike<ValueType> | ValueType>,
  50. // mapper: (
  51. // element: ValueType,
  52. // index: number
  53. // ) => PromiseLike<MappedValueType> | MappedValueType
  54. // ): Promise<MappedValueType[]>;
  55. // export = pMapSeries;
  56. default: typeof pMapSeries;
  57. };
  58. export = pMapSeries;