Skip to content

Instantly share code, notes, and snippets.

@friendlyanon
Created December 11, 2018 16:58
Show Gist options
  • Save friendlyanon/fd2f9efdf52720797bc815487fc1aef6 to your computer and use it in GitHub Desktop.
Save friendlyanon/fd2f9efdf52720797bc815487fc1aef6 to your computer and use it in GitHub Desktop.
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