UNPKG

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