UNPKG

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