Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- const myPromise = new PromiseSimple((onResolve, onReject) => {
- setTimeout(onResolve, 1000, 'Fine!');
- });
- myPromise.then((response) => {
- console.log(response);
- }, (error) => {
- console.log(error);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement