Skip to content

Instantly share code, notes, and snippets.

@mattisa
Created April 16, 2020 06:13
Show Gist options
  • Save mattisa/4eca326a16549c4480ba5e8594f95c38 to your computer and use it in GitHub Desktop.
Save mattisa/4eca326a16549c4480ba5e8594f95c38 to your computer and use it in GitHub Desktop.
export default function debouncePromise(fn, delay) {
let time = null;
return (...args) => {
if (time) {
clearTimeout(time);
}
return new Promise((res, rej) => {
time = setTimeout(() => {
fn.apply(null, args)
.then((...resArgs) => res.apply(null, resArgs))
.catch(e => rej(e));
}, delay);
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment