Last active
November 23, 2018 09:02
-
-
Save trygvea/738171e61ca81812d3029649cece758e to your computer and use it in GitHub Desktop.
debouncePromise
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
import {debounce} from 'lodash'; // or whatever | |
const debouncePromise = <T>(fn: (...args) => Promise<T>, wait: number, options = {}): ((...args) => Promise<T>) => { | |
return (...args) => | |
new Promise((resolve, reject) => { | |
const promisedFn = (...args) => | |
fn(...args) | |
.then(resolve) | |
.catch(reject); | |
const debouncedPromisedFn = debounce(promisedFn, wait, options); | |
debouncedPromisedFn(...args); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment