Skip to content

Instantly share code, notes, and snippets.

@deadkff01
Created January 21, 2019 01:30
Show Gist options
  • Save deadkff01/4edc7a654cb29bf73388098a923e7eed to your computer and use it in GitHub Desktop.
Save deadkff01/4edc7a654cb29bf73388098a923e7eed to your computer and use it in GitHub Desktop.
Simple defer imlementation
class Deferred {
constructor(){
this.canceled = false
this.promise = new Promise((resolve, reject) => {
this.resolve = (value) => {
if(!this.canceled) resolve(value)
}
this.reject = (value) => {
if(!this.canceled) reject(value)
}
})
}
resolve(value){
this.resolve(value)
}
reject(value){
this.reject(value)
}
then(onFulfilled, onRejected){
this.promise.then(onFulfilled, onRejected)
}
cancel(){
this.canceled = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment