Created
January 21, 2019 01:30
-
-
Save deadkff01/4edc7a654cb29bf73388098a923e7eed to your computer and use it in GitHub Desktop.
Simple defer imlementation
This file contains hidden or 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.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