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.resolve()` it
|
12
|
const testElement = (el, tester) => Promise.resolve(el).then(tester);
|
13
|
|
14
|
// The input can also be a promise, so we `Promise.all()` them both
|
15
|
const finder = el => Promise.all(el).then(val => val[1] === true && Promise.reject(new EndError(val[0])));
|
16
|
|
17
|
module.exports = (iterable, tester, opts) => {
|
18
|
opts = Object.assign({
|
19
|
concurrency: Infinity,
|
20
|
preserveOrder: true
|
21
|
}, opts);
|
22
|
|
23
|
const limit = pLimit(opts.concurrency);
|
24
|
|
25
|
// Start all the promises concurrently with optional limit
|
26
|
const items = [...iterable].map(el => [el, limit(testElement, el, tester)]);
|
27
|
|
28
|
// Check the promises either serially or concurrently
|
29
|
const checkLimit = pLimit(opts.preserveOrder ? 1 : Infinity);
|
30
|
|
31
|
return Promise.all(items.map(el => checkLimit(finder, el)))
|
32
|
.then(() => {})
|
33
|
.catch(err => err instanceof EndError ? err.value : Promise.reject(err));
|
34
|
};
|