index.d.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. declare const stop: unique symbol;
  2. declare namespace pEachSeries {
  3. type StopSymbol = typeof stop;
  4. }
  5. declare const pEachSeries: {
  6. /**
  7. Iterate over promises serially.
  8. @param input - Iterated over serially in the `iterator` function.
  9. @param iterator - Return value is ignored unless it's `Promise`, then it's awaited before continuing with the next iteration.
  10. @returns A `Promise` that fulfills when all promises in `input` and ones returned from `iterator` are fulfilled, or rejects if any of the promises reject. The fulfillment value is the original `input`.
  11. @example
  12. ```
  13. import pEachSeries = require('p-each-series');
  14. const keywords = [
  15. getTopKeyword(), //=> Promise
  16. 'rainbow',
  17. 'pony'
  18. ];
  19. const iterator = async element => saveToDiskPromise(element);
  20. (async () => {
  21. console.log(await pEachSeries(keywords, iterator));
  22. //=> ['unicorn', 'rainbow', 'pony']
  23. })();
  24. ```
  25. */
  26. <ValueType>(
  27. input: Iterable<PromiseLike<ValueType> | ValueType>,
  28. iterator: (element: ValueType, index: number) => pEachSeries.StopSymbol | unknown
  29. ): Promise<ValueType[]>;
  30. /**
  31. Stop iterating through items by returning `pEachSeries.stop` from the iterator function.
  32. @example
  33. ```
  34. const pEachSeries = require('p-each-series');
  35. // Logs `a` and `b`.
  36. const result = await pEachSeries(['a', 'b', 'c'], value => {
  37. console.log(value);
  38. if (value === 'b') {
  39. return pEachSeries.stop;
  40. }
  41. });
  42. console.log(result);
  43. //=> ['a', 'b', 'c']
  44. ```
  45. */
  46. readonly stop: pEachSeries.StopSymbol;
  47. // TODO: Remove this for the next major release
  48. default: typeof pEachSeries;
  49. };
  50. export = pEachSeries;