Skip to content
Permalink
Newer
Older
100644 49 lines (38 sloc) 1.35 KB
Ignoring revisions in .git-blame-ignore-revs.
February 1, 2022 18:01
1
export interface Options {
February 1, 2022 18:01
3
The number of concurrently pending promises returned by `tester`.
February 1, 2022 18:01
5
Minimum: `1`
February 1, 2022 18:01
7
@default Infinity
8
*/
9
readonly concurrency?: number;
February 1, 2022 18:01
11
/**
12
Preserve `input` order when searching.
February 1, 2022 18:01
14
Disable this to improve performance if you don't care about the order.
February 1, 2022 18:01
16
@default true
February 1, 2022 18:01
18
readonly preserveOrder?: boolean;
19
}
February 1, 2022 18:01
21
/**
22
Get 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
```
30
import {pathExists} from 'path-exists';
31
import pLocate from 'p-locate';
32
33
const files = [
34
'unicorn.png',
35
'rainbow.png', // Only this one actually exists on disk
36
'pony.png'
37
];
38
39
const foundPath = await pLocate(files, file => pathExists(file));
40
41
console.log(foundPath);
42
//=> 'rainbow'
43
```
44
*/
45
export default function pLocate<ValueType>(
46
input: Iterable<PromiseLike<ValueType> | ValueType>,
47
tester: (element: ValueType) => PromiseLike<boolean> | boolean,
48
options?: Options
49
): Promise<ValueType | undefined>;