Skip to content

Instantly share code, notes, and snippets.

@ayamflow
Created January 24, 2022 13:50
Show Gist options
  • Save ayamflow/6c476e7974cda5d6ff6d941945a208a2 to your computer and use it in GitHub Desktop.
Save ayamflow/6c476e7974cda5d6ff6d941945a208a2 to your computer and use it in GitHub Desktop.
js deferred using promise
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