From e4f12b16050f689e6201a81347dec48aab2eaaa1 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 11 Oct 2016 09:17:05 +0300 Subject: [PATCH] 108 passed tests --- lib/SuperPromise.js | 157 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 144 insertions(+), 13 deletions(-) diff --git a/lib/SuperPromise.js b/lib/SuperPromise.js index bc50312..4b6698b 100644 --- a/lib/SuperPromise.js +++ b/lib/SuperPromise.js @@ -1,39 +1,170 @@ 'use strict'; // NO REQUIRE +let State = { + PENDING: 0, + RESOLVED: 1, + REJECTED: 2 +}; + +var count = 0; + class SuperPromise { + constructor(executor) { + count += 1; + //console.log(count); + + this.executor = executor; + this.result = undefined; + this.error = undefined; + this.state = State.PENDING; + this.resolvers = []; + this.rejecters = []; + + + executor((data) => { + this.resolveWithResult(data); + }, (error) => { + this.rejectWithReason(error); + }); + } + + resolveWithResult(result) { + if (this.state == State.PENDING) { + this.result = result; + this.state = State.RESOLVED; + + setTimeout(() => { + for (var i = 0; i < this.resolvers.length; i++) { + this.resolvers[i](result); + } + }, 0); + } } - then(onResolve) { + rejectWithReason(reason) { + if (this.state == State.PENDING) { + this.error = reason; + this.state = State.REJECTED; + setTimeout(() => { + this.rejecters.forEach(rejecter => rejecter(reason)); + }, 0) + } } - catch(onRejection) { + then(onResolved, onRejected) { + if (onResolved == false || onResolved == undefined) { + onResolved = (arg) => { + }; + } + + if (!onRejected) { + onRejected = (arg) => { + }; + } + + if (this.state == State.PENDING) { + this.resolvers.push(onResolved); + this.rejecters.push(onRejected); + } + else { + if (this.state == State.REJECTED) { + + try { + setTimeout(() => { + onRejected(this.error); + }, 0); + return SuperPromise.reject(this.error); + } + catch (e) { + setTimeout(() => { + onRejected(e); + }, 0); + return SuperPromise.reject(e); + } + } + else { // if state == RESOLVED + try { + setTimeout(() => { + onResolved(this.result); + }, 0); + return SuperPromise.resolve(this.result); + } + catch (e) { + setTimeout(() => { + onRejected(e); + }, 0); + return SuperPromise.reject(e); + } + } + } + return new SuperPromise((arg1, arg2) => { + }); } - /* PART 2 */ - static resolve() { - // Your code here... + catch(onRejected) { + return this.then(null, onRejected); } - static reject() { - // Your code here... + static resolve(result) { + return new SuperPromise((resolve, _) => { + try { + resolve(result); + } + catch (e) { + this.reject(e); + } + }); + + } + + static reject(reason) { + return new SuperPromise((_, reject) => { + try { + reject(reason); + } + catch (e) { + reject(e); + } + }); } - static all() { - // Your code here... + static all(promises) { + return new SuperPromise((resolve, reject) => { + var totalResult = []; + + for (let promise of promises) { + promise.then(result => { + totalResult.push(result); + if (totalResult.length == promises.length) { + resolve(totalResult); + } + }).catch(error => { + reject(error); + }) + } + }) } - static race() { - // Your code here... + static race(promises) { + return new SuperPromise((resolve, reject) => { + for (let promise of promises) { + promise.then(result => { + resolve(result); + }) + } + }) } /* PART 3 */ - static queue() { + static queue(promises) { + return new SuperPromise((resolve, reject) => { + }); } static stack() { @@ -41,4 +172,4 @@ class SuperPromise { } } -module.exports = Promise; // TODO: kek +module.exports = SuperPromise; // TODO: kek