"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _castArray=_interopRequireDefault(require("lodash/castArray"));

var _isPositiveInteger=_interopRequireDefault(require("./isPositiveInteger"));
var _racePromises=_interopRequireDefault(require("./racePromises"));var _default=

(promises,limit=Infinity)=>{
const len=promises.length;

if(limit>len){
limit=len;
}

if(!(0,_isPositiveInteger.default)(limit)&&limit!==Infinity){
throw new Error(`limit should be a positive integer or Infinity. Instead got: ${limit}`);
}

if(limit===1){
return(0,_racePromises.default)(promises).then(_castArray.default);
}

return new Promise((resolve,reject)=>{
const positions=[];
for(let i=0;i<len;++i){
promises[i].
then(()=>{
positions.push(i);
if(positions.length===limit){
resolve(positions.slice());
}
}).
catch(reject);
}
});
};exports.default=_default;