index.d.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. declare namespace pReduce {
  2. type ReducerFunction<ValueType, ReducedValueType = ValueType> = (
  3. previousValue: ReducedValueType,
  4. currentValue: ValueType,
  5. index: number
  6. ) => PromiseLike<ReducedValueType> | ReducedValueType;
  7. }
  8. declare const pReduce: {
  9. /**
  10. Reduce a list of values using promises into a promise for a value.
  11. @param input - Iterated over serially in the `reducer` function.
  12. @param reducer - Expected to return a value. If a `Promise` is returned, it's awaited before continuing with the next iteration.
  13. @param initialValue - Value to use as `previousValue` in the first `reducer` invocation.
  14. @returns A `Promise` that is fulfilled when all promises in `input` and ones returned from `reducer` are fulfilled, or rejects if any of the promises reject. The resolved value is the result of the reduction.
  15. @example
  16. ```
  17. import pReduce = require('p-reduce');
  18. import humanInfo from 'human-info'; // Not a real module
  19. (async () => {
  20. const names = [
  21. getUser('sindresorhus').then(info => info.name),
  22. 'Addy Osmani',
  23. 'Pascal Hartig',
  24. 'Stephen Sawchuk'
  25. ];
  26. const totalAge = await pReduce(names, async (total, name) => {
  27. const info = await humanInfo(name);
  28. return total + info.age;
  29. }, 0);
  30. console.log(totalAge);
  31. //=> 125
  32. })();
  33. ```
  34. */
  35. <ValueType, ReducedValueType = ValueType>(
  36. input: Iterable<PromiseLike<ValueType> | ValueType>,
  37. reducer: pReduce.ReducerFunction<ValueType, ReducedValueType>,
  38. initialValue?: ReducedValueType
  39. ): Promise<ReducedValueType>;
  40. // TODO: Remove this for the next major release, refactor the whole definition to:
  41. // declare function pReduce<ValueType, ReducedValueType = ValueType>(
  42. // input: Iterable<PromiseLike<ValueType> | ValueType>,
  43. // reducer: pReduce.ReducerFunction<ValueType, ReducedValueType>,
  44. // initialValue?: ReducedValueType
  45. // ): Promise<ReducedValueType>;
  46. // export = pReduce;
  47. default: typeof pReduce;
  48. };
  49. export = pReduce;