index.d.ts 1.3 KB

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