Skip to content

Instantly share code, notes, and snippets.

@trygvea
Last active November 23, 2018 09:02
Show Gist options
  • Save trygvea/738171e61ca81812d3029649cece758e to your computer and use it in GitHub Desktop.
Save trygvea/738171e61ca81812d3029649cece758e to your computer and use it in GitHub Desktop.
debouncePromise
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