mirror of
https://github.com/transloadit/uppy.git
synced 2026-07-23 02:08:30 +00:00
And rewrite all the require calls. ```bash git mv src/utils/* packages/@uppy/utils/src sed -i 's/[./]*\/utils\//@uppy\/utils\/lib\//' src/**/*.js # transform (../)*utils → @uppy/utils ```
36 lines
834 B
JavaScript
36 lines
834 B
JavaScript
/**
|
|
* Limit the amount of simultaneously pending Promises.
|
|
* Returns a function that, when passed a function `fn`,
|
|
* will make sure that at most `limit` calls to `fn` are pending.
|
|
*
|
|
* @param {number} limit
|
|
* @return {function()}
|
|
*/
|
|
module.exports = function limitPromises (limit) {
|
|
let pending = 0
|
|
const queue = []
|
|
return (fn) => {
|
|
return (...args) => {
|
|
const call = () => {
|
|
pending++
|
|
const promise = fn(...args)
|
|
promise.then(onfinish, onfinish)
|
|
return promise
|
|
}
|
|
|
|
if (pending >= limit) {
|
|
return new Promise((resolve, reject) => {
|
|
queue.push(() => {
|
|
call().then(resolve, reject)
|
|
})
|
|
})
|
|
}
|
|
return call()
|
|
}
|
|
}
|
|
function onfinish () {
|
|
pending--
|
|
const next = queue.shift()
|
|
if (next) next()
|
|
}
|
|
}
|