Created
January 24, 2022 13:50
-
-
Save ayamflow/6c476e7974cda5d6ff6d941945a208a2 to your computer and use it in GitHub Desktop.
js deferred using promise
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
class Deferred { | |
constructor() { | |
this.promise = new Promise((resolve, reject) => { | |
this.resolve = resolve | |
this.reject = reject | |
}) | |
this.then = this.promise.then.bind(this.promise) | |
this.catch = this.promise.catch.bind(this.promise) | |
} | |
} | |
export function deferred() { | |
return new Deferred() | |
} | |
// await timeout(100) | |
export function timeout(duration = 0) { | |
return new Promise(resolve => { | |
setTimeout(resolve, duration) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment