import pLimit from "p-limit" ; interface IOptions { concurrency ? : number ; onRes ? : ( limit : any ) => void ; } interface IPromiseTask { task : ( ... rest : any [ ] ) => Promise < any > ; arguments : any [ ] ; } function promiseThrottle ( promises , options ) { const { concurrency = 2 , onRes } = options ; const limit = pLimit ( concurrency ) ; return Promise . all ( promises . map ( promise => limit ( ( ) => promise . task ( ... promise . arguments ) . then ( res => res ) ) . then ( ( ) => onRes ( limit ) ) ) ) ; } export default promiseThrottle ;