index.d.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. /**
  17. Get the first fulfilled promise that satisfies the provided testing function.
  18. @param input - An iterable of promises/values to test.
  19. @param tester - This function will receive resolved values from `input` and is expected to return a `Promise<boolean>` or `boolean`.
  20. @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`.
  21. @example
  22. ```
  23. import pathExists = require('path-exists');
  24. import pLocate = require('p-locate');
  25. const files = [
  26. 'unicorn.png',
  27. 'rainbow.png', // Only this one actually exists on disk
  28. 'pony.png'
  29. ];
  30. (async () => {
  31. const foundPath = await pLocate(files, file => pathExists(file));
  32. console.log(foundPath);
  33. //=> 'rainbow'
  34. })();
  35. ```
  36. */
  37. declare function pLocate<ValueType>(
  38. input: Iterable<PromiseLike<ValueType> | ValueType>,
  39. tester: (element: ValueType) => PromiseLike<boolean> | boolean,
  40. options?: pLocate.Options
  41. ): Promise<ValueType | undefined>;
  42. export = pLocate;