// https://stackoverflow.com/questions/35228052/debounce-function-implemented-with-promises
function debounce (inner, ms = 0) {
let timer = null
let resolves = []
return function (...args) {
// Run the function after a certain amount of time
clearTimeout(timer)
timer = setTimeout(() => {
// Get the result of the inner function, then apply it to the resolve function of
// each promise that has been created since the last time the inner function was run
const result = inner(...args)
resolves.forEach(r => r(result))
resolves = []
}, ms)
return new Promise(resolve => resolves.push(resolve))
}
}
Forked from zmts/debounce-function-implemented-with-promises.md
Created
January 10, 2021 19:29
-
-
Save FoxMalder/d77562c05c7a196ad973acc1ad66bb0a to your computer and use it in GitHub Desktop.
Debounce with promises
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment