Skip to content

Instantly share code, notes, and snippets.

@Tomotoes
Created February 26, 2020 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tomotoes/0ba98c8d2b88f87baa82832542a780de to your computer and use it in GitHub Desktop.
Save Tomotoes/0ba98c8d2b88f87baa82832542a780de to your computer and use it in GitHub Desktop.
PromiseSimple
class PromiseSimple {
constructor(executionFunction) {
this.promiseChain = [];
this.handleError = () => {};
this.onResolve = this.onResolve.bind(this);
this.onReject = this.onReject.bind(this);
executionFunction(this.onResolve, this.onReject);
}
then(onResolve) {
this.promiseChain.push(onResolve);
return this;
}
catch(handleError) {
this.handleError = handleError;
return this;
}
onResolve(value) {
let storedValue = value;
try {
this.promiseChain.forEach((nextFunction) => {
storedValue = nextFunction(storedValue);
});
} catch (error) {
this.promiseChain = [];
this.onReject(error);
}
}
onReject(error) {
this.handleError(error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment