Created
December 11, 2018 16:58
-
-
Save friendlyanon/fd2f9efdf52720797bc815487fc1aef6 to your computer and use it in GitHub Desktop.
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 ReusablePromise { | |
constructor() { | |
this.executor = this._executor.bind(this); | |
this.resolve = | |
this.reject = | |
this.root = | |
this.args = | |
this.method = null; | |
} | |
setup(root, method, ...args) { | |
this.root = root; | |
this.args = args; | |
this.method = method; | |
return this; | |
} | |
then(resolve, reject) { | |
this.resolve = resolve; | |
this.reject = reject; | |
if (this.args) this.method.call(this.root, ...this.args, this.executor); | |
else this.method.call(this.root, this.executor); | |
} | |
// for node style (error, result) callback style async functions | |
_executor(err, result) { | |
const { resolve, reject } = this; | |
this.resolve = | |
this.reject = | |
this.args = null; | |
if (err) reject(err); | |
else resolve(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment