Created
June 17, 2019 16:30
-
-
Save joshuaedwardcrowe/3a0ab386e98ce374b51de941dbab55b4 to your computer and use it in GitHub Desktop.
Delay the execution of asynchronous functions.
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
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