'use strict';constpLimit=require('p-limit');classEndErrorextendsError{constructor(value){super();this.value=value;}}// the input can also be a promise, so we `Promise.all()` them bothconstfinder=el=>Promise.all(el).then(val=>val[1]===true&&Promise.reject(newEndError(val[0])));module.exports=(iterable,tester,opts)=>{opts=Object.assign({concurrency:Infinity,preserveOrder:true},opts);constlimit=pLimit(opts.concurrency);// start all the promises concurrently with optional limitconstitems=Array.from(iterable).map(el=>[el,limit(()=>Promise.resolve(el).then(tester))]);// check the promises either serially or concurrentlyconstcheckLimit=pLimit(opts.preserveOrder?1:Infinity);returnPromise.all(items.map(el=>checkLimit(()=>finder(el)))).then(()=>{}).catch(err=>errinstanceofEndError?err.value:Promise.reject(err));};