1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| const States = { PENDING: "PENDING", RESOLVED: "RESOLVED", REJECTED: "REJECTED" };
class _Promise { constructor(callback) { this.state = States.PENDING; this.value = void 0; this.interrupt = false; this.resolvedHandlers = []; this.rejectedHandlers = []; try { callback.call(this, this._resolve.bind(this), this._reject.bind(this)); } catch (e) { this.interrupt = true; this._reject.call(this, e); } } _resolve(value) { this._setValue(value, States.RESOLVED); } _reject(value) { this._setValue(value, States.REJECTED); } _setValue(value, state) { if (this.state !== States.PENDING) { return; } this.state = state; this.value = value; setTimeout(this._executeHandler.bind(this), 0); } _executeHandler() { if (this.state === "PENDING") { return null; } const res = this.state === States.RESOLVED ? this.resolvedHandlers.forEach(handler => handler()) : this.rejectedHandlers.forEach(handler => handler()); return res; } _isThenable(obj) { return obj.then && typeof obj.then === "function"; } _promiseCheck(x, resolve, reject, nextPromise) { if (x && x === nextPromise) throw new TypeError(`${x} can not be same as ${nextPromise}.`); else if (x instanceof _Promise) { x.then( res => nextPromise._promiseCheck(res, resolve, reject, nextPromise), reject ).catch(reject); } else if (x && (typeof x === "function" || typeof x === "object")) { const then = x.then; if (typeof then === "function") { then.apply( x, function resolvePromise(y) { if (y === x) throw new TypeError("Promise recursion!"); nextPromise._promiseCheck(y, resolve, reject, nextPromise); }, function rejectPromise(r) { reject(r); } ); } else { resolve(x); } } else resolve(x); }
then(onFulfilled = res => res, onRejected) { const pendingThen = (resolve, reject, newPromise) => { this.resolvedHandlers.push(() => { try { const temple = onFulfilled(this.value); this._promiseCheck(temple, resolve, reject, newPromise); } catch (error) { reject(error); } }); this.rejectedHandlers.push(() => { try { typeof onRejected !== "function" && this.interrupt ? reject(this.value) : this._promiseCheck( onRejected(this.value), resolve, reject, newPromise ); } catch (error) { reject(error); } }); }; const resolvedThen = (resolve, reject) => { setTimeout(() => { try { const temple = onFulfilled(this.value); this._promiseCheck(temple, resolve, reject, newPromise); } catch (error) { reject(error); } }, 0); }; const rejectedThen = (resolve, reject) => { setTimeout(() => { try { typeof onRejected !== "function" && this.interrupt ? reject(this.value) : this._promiseCheck( onRejected(this.value), resolve, reject, newPromise ); } catch (error) { reject(error); } }, 0); }; let nextPromise; nextPromise = new _Promise((resolve, reject) => { switch (this.state) { case States.PENDING: pendingThen(resolve, reject, nextPromise); break; case States.RESOLVED: resolvedThen(resolve, reject, nextPromise); break; case States.REJECTED: rejectedThen(resolve, reject, nextPromise); } }); return nextPromise; } }
|