1 | # p-locate [![Build Status](https://travis-ci.com/sindresorhus/p-locate.svg?branch=master)](https://travis-ci.com/github/sindresorhus/p-locate)
|
---|
2 |
|
---|
3 | > Get the first fulfilled promise that satisfies the provided testing function
|
---|
4 |
|
---|
5 | Think of it like an async version of [`Array#find`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find).
|
---|
6 |
|
---|
7 | ## Install
|
---|
8 |
|
---|
9 | ```
|
---|
10 | $ npm install p-locate
|
---|
11 | ```
|
---|
12 |
|
---|
13 | ## Usage
|
---|
14 |
|
---|
15 | Here we find the first file that exists on disk, in array order.
|
---|
16 |
|
---|
17 | ```js
|
---|
18 | const pathExists = require('path-exists');
|
---|
19 | const pLocate = require('p-locate');
|
---|
20 |
|
---|
21 | const files = [
|
---|
22 | 'unicorn.png',
|
---|
23 | 'rainbow.png', // Only this one actually exists on disk
|
---|
24 | 'pony.png'
|
---|
25 | ];
|
---|
26 |
|
---|
27 | (async () => {
|
---|
28 | const foundPath = await pLocate(files, file => pathExists(file));
|
---|
29 |
|
---|
30 | console.log(foundPath);
|
---|
31 | //=> 'rainbow'
|
---|
32 | })();
|
---|
33 | ```
|
---|
34 |
|
---|
35 | *The above is just an example. Use [`locate-path`](https://github.com/sindresorhus/locate-path) if you need this.*
|
---|
36 |
|
---|
37 | ## API
|
---|
38 |
|
---|
39 | ### pLocate(input, tester, options?)
|
---|
40 |
|
---|
41 | 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`.
|
---|
42 |
|
---|
43 | #### input
|
---|
44 |
|
---|
45 | Type: `Iterable<Promise | unknown>`
|
---|
46 |
|
---|
47 | An iterable of promises/values to test.
|
---|
48 |
|
---|
49 | #### tester(element)
|
---|
50 |
|
---|
51 | Type: `Function`
|
---|
52 |
|
---|
53 | This function will receive resolved values from `input` and is expected to return a `Promise<boolean>` or `boolean`.
|
---|
54 |
|
---|
55 | #### options
|
---|
56 |
|
---|
57 | Type: `object`
|
---|
58 |
|
---|
59 | ##### concurrency
|
---|
60 |
|
---|
61 | Type: `number`\
|
---|
62 | Default: `Infinity`\
|
---|
63 | Minimum: `1`
|
---|
64 |
|
---|
65 | Number of concurrently pending promises returned by `tester`.
|
---|
66 |
|
---|
67 | ##### preserveOrder
|
---|
68 |
|
---|
69 | Type: `boolean`\
|
---|
70 | Default: `true`
|
---|
71 |
|
---|
72 | Preserve `input` order when searching.
|
---|
73 |
|
---|
74 | Disable this to improve performance if you don't care about the order.
|
---|
75 |
|
---|
76 | ## Related
|
---|
77 |
|
---|
78 | - [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
|
---|
79 | - [p-filter](https://github.com/sindresorhus/p-filter) - Filter promises concurrently
|
---|
80 | - [p-any](https://github.com/sindresorhus/p-any) - Wait for any promise to be fulfilled
|
---|
81 | - [More…](https://github.com/sindresorhus/promise-fun)
|
---|
82 |
|
---|
83 | ---
|
---|
84 |
|
---|
85 | <div align="center">
|
---|
86 | <b>
|
---|
87 | <a href="https://tidelift.com/subscription/pkg/npm-p-locate?utm_source=npm-p-locate&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
---|
88 | </b>
|
---|
89 | <br>
|
---|
90 | <sub>
|
---|
91 | Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
---|
92 | </sub>
|
---|
93 | </div>
|
---|