Created
January 30, 2018 00:40
-
-
Save beautyfree/2f974c10966da64887c146c4e075c458 to your computer and use it in GitHub Desktop.
Rate limit for api requests from https://github.com/axios/axios/issues/230
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function promiseDebounce(fn, delay, count) { | |
var working = 0, queue = [] | |
function work() { | |
if ((queue.length === 0) || (working === count)) return | |
working++ | |
new Promise(function(resolve, reject){ | |
setTimeout(function(){ | |
working-- | |
work() | |
}, delay) | |
}) | |
var next = queue.shift() | |
next[2](fn.apply(next[0], next[1])) | |
} | |
return function debounced() { | |
var args = arguments | |
return new Promise(function(resolve){ | |
queue.push([this, args, resolve]) | |
if (working < count) work() | |
}.bind(this)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment