Skip to content

Instantly share code, notes, and snippets.

@joshuaedwardcrowe
Created June 17, 2019 16:30
Show Gist options
  • Save joshuaedwardcrowe/3a0ab386e98ce374b51de941dbab55b4 to your computer and use it in GitHub Desktop.
Save joshuaedwardcrowe/3a0ab386e98ce374b51de941dbab55b4 to your computer and use it in GitHub Desktop.
Delay the execution of asynchronous functions.
export class Debouncer {
public promiseGenerator: () => Promise;
public delayInMilliseconds: number;
private timeoutCompleted: bool;
private timeoutCached: () => void;
constructor (generator: () => Promise, delayInMilliseconds: number) {
this.promiseGenerator = generator;
this.delayInMilliseconds = delayInMilliseconds;
this.timeoutCompleted = false;
}
public async attempt () {
if (this.timeoutCompleted)
return this.promiseGenerator(args);
this.timeoutCached = new setTimeout(
() => this.timeoutCompleted = true,
this.delayInMilliseconds)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment