function makeTriggerablePromise < T > ( ) { let triggerResolveWith ! ; let triggerRejectWith ! ; const promToReturn = new Promise ( ( resolve , reject ) => { const funcThatResolvesProm = ( inp ) => resolve ( inp ) ; triggerResolveWith = funcThatResolvesProm ; triggerRejectWith = reject ; } ) ; return [ promToReturn , triggerResolveWith , triggerRejectWith ] ; } export class Qew { private queue : ( ( ) => void ) [ ] = [ ] ; private executing = 0 ; param maxConcurrent param delay constructor ( private maxConcurrent = 1 , private delay = 0 ) { if ( maxConcurrent < 1 ) { throw new Error ( "maxConcurrent has to be 1 or higher" ) ; } if ( typeof delay === "number" && delay < 0 ) { throw new Error ( "`delay` parameter should be either a non-negative number or a function that returns one" ) ; } } param asyncFunc returns public push < T > ( asyncFunc ) { const [ prom , resolveProm , rejectProm ] = makeTriggerablePromise < T > ( ) ; const funcToRun = ( ) => { asyncFunc ( ) . then ( ( result ) => { resolveProm ( result ) ; this . executing = this . executing - 1 ; const delay = typeof this . delay === "function" ? this . delay ( ) : this . delay ; setTimeout ( this . tryMove , delay ) ; } ) . catch ( rejectProm ) ; } ; this . queue . push ( funcToRun ) ; this . tryMove ( ) ; return prom ; } deprecated public pushProm = this . push ; private tryMove ( ) { if ( this . executing < this . maxConcurrent ) { const first = this . queue . shift ( ) ; if ( first ) { this . executing = this . executing + 1 ; first ( ) ; } } } } export default Qew ;