2 const pLimit = require('p-limit');
4 class EndError extends Error {
11 // The input can also be a promise, so we `Promise.resolve()` it
12 const testElement = (el, tester) => Promise.resolve(el).then(tester);
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])));
17 module.exports = (iterable, tester, opts) => {
18 opts = Object.assign({
19 concurrency: Infinity,
23 const limit = pLimit(opts.concurrency);
25 // Start all the promises concurrently with optional limit
26 const items = [...iterable].map(el => [el, limit(testElement, el, tester)]);
28 // Check the promises either serially or concurrently
29 const checkLimit = pLimit(opts.preserveOrder ? 1 : Infinity);
31 return Promise.all(items.map(el => checkLimit(finder, el)))
33 .catch(err => err instanceof EndError ? err.value : Promise.reject(err));