index.d.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. declare namespace pLocate {
  2. interface Options {
  3. /**
  4. Number of concurrently pending promises returned by `tester`. Minimum: `1`.
  5. @default Infinity
  6. */
  7. readonly concurrency?: number;
  8. /**
  9. Preserve `input` order when searching.
  10. Disable this to improve performance if you don't care about the order.
  11. @default true
  12. */
  13. readonly preserveOrder?: boolean;
  14. }
  15. }
  16. declare const pLocate: {
  17. /**
  18. Get the first fulfilled promise that satisfies the provided testing function.
  19. @param input - An iterable of promises/values to test.
  20. @param tester - This function will receive resolved values from `input` and is expected to return a `Promise<boolean>` or `boolean`.
  21. @returns A `Promise` that is fulfilled when `tester` resolves to `true` or the iterable is done, or rejects if any of the promises reject. The fulfilled value is the current iterable value or `undefined` if `tester` never resolved to `true`.
  22. @example
  23. ```
  24. import pathExists = require('path-exists');
  25. import pLocate = require('p-locate');
  26. const files = [
  27. 'unicorn.png',
  28. 'rainbow.png', // Only this one actually exists on disk
  29. 'pony.png'
  30. ];
  31. (async () => {
  32. const foundPath = await pLocate(files, file => pathExists(file));
  33. console.log(foundPath);
  34. //=> 'rainbow'
  35. })();
  36. ```
  37. */
  38. <ValueType>(
  39. input: Iterable<PromiseLike<ValueType> | ValueType>,
  40. tester: (element: ValueType) => PromiseLike<boolean> | boolean,
  41. options?: pLocate.Options
  42. ): Promise<ValueType | undefined>;
  43. // TODO: Remove this for the next major release, refactor the whole definition to:
  44. // declare function pLocate<ValueType>(
  45. // input: Iterable<PromiseLike<ValueType> | ValueType>,
  46. // tester: (element: ValueType) => PromiseLike<boolean> | boolean,
  47. // options?: pLocate.Options
  48. // ): Promise<ValueType | undefined>;
  49. // export = pLocate;
  50. default: typeof pLocate;
  51. };
  52. export = pLocate;