.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / p-locate / index.js
1 'use strict';
2 const pLimit = require('p-limit');
3
4 class EndError extends Error {
5         constructor(value) {
6                 super();
7                 this.value = value;
8         }
9 }
10
11 // the input can also be a promise, so we `Promise.all()` them both
12 const finder = el => Promise.all(el).then(val => val[1] === true && Promise.reject(new EndError(val[0])));
13
14 module.exports = (iterable, tester, opts) => {
15         opts = Object.assign({
16                 concurrency: Infinity,
17                 preserveOrder: true
18         }, opts);
19
20         const limit = pLimit(opts.concurrency);
21
22         // start all the promises concurrently with optional limit
23         const items = Array.from(iterable).map(el => [el, limit(() => Promise.resolve(el).then(tester))]);
24
25         // check the promises either serially or concurrently
26         const checkLimit = pLimit(opts.preserveOrder ? 1 : Infinity);
27
28         return Promise.all(items.map(el => checkLimit(() => finder(el))))
29                 .then(() => {})
30                 .catch(err => err instanceof EndError ? err.value : Promise.reject(err));
31 };