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
| class TPromise { static PENDING = 'PENDING'; static RESOLVED = 'RESOLVED'; static REJECTED = 'REJECTED';
constructor(handler) { this.resolveQueues = []; this.rejectQueues = []; this.finallyQueues = []; this.state = TPromise.PENDING; handler(this._resolve.bind(this), this._reject.bind(this)); }
_resolve(val) { let observer = new MutationObserver(() => { if (this.state !== TPromise.PENDING) return; this.state = TPromise.RESOLVED; this.value = val; let handler; while ((handler = this.resolveQueues.shift())) { handler(this.value); } while ((handler = this.finallyQueues.shift())) { handler(); } });
observer.observe(document.body, { attributes: true }); document.body.setAttribute('_promise', Date.now()); }
_reject(val) { let observer = new MutationObserver(() => { if (this.state !== TPromise.PENDING) return; this.state = TPromise.REJECTED; this.value = val; let handler; while ((handler = this.rejectQueues.shift())) { handler(this.value); } while ((handler = this.finallyQueues.shift())) { handler(); } });
observer.observe(document.body, { attributes: true }); document.body.setAttribute('_promise', Date.now()); }
then(resolveHandler, rejectHandler) { return new TPromise((resolve, reject) => { if (typeof resolveHandler === 'function') { const newResolveHandler = val => { let result = resolveHandler(val); if (result instanceof TPromise) { result.then(resolve, reject); } else { resolve(result); } }; this.resolveQueues.push(newResolveHandler); } else { this.resolveQueues.push(resolve); }
if (typeof rejectHandler === 'function') { const newRejectHandler = val => { let result = rejectHandler(val); if (result instanceof TPromise) { result.then(resolve, reject); } else { resolve(result); } }; this.rejectQueues.push(newRejectHandler); } else { this.rejectQueues.push(reject); } }); }
catch(rejectHandler) { return this.then(undefined, rejectHandler); }
finally(finallyHandler) { return new TPromise((resolve, reject) => { if (typeof finallyHandler === 'function') { const newFinallyHandler = () => { const result = finallyHandler(); if (result instanceof TPromise) { result.finally(() => { if (this.state === TPromise.RESOLVED) resolve(this.value); else if (this.state === TPromise.REJECTED) reject(this.value); }); } else { if (this.state === TPromise.RESOLVED) resolve(this.value); else if (this.state === TPromise.REJECTED) reject(this.value); } }; this.finallyQueues.push(newFinallyHandler); } }); }
static all(arr) { let i = 0; const resArr = []; return new TPromise((resolve, reject) => { arr.forEach((it, index) => { it.then(res => { i++; resArr[index] = res; if (i >= arr.length) { resolve(resArr); } }).catch(err => { reject(err); }); }); }); }
static race(arr) { return new TPromise((resolve, reject) => { arr.forEach((it, index) => { it.then(res => { resolve(res); }).catch(err => { reject(err); }); }); }); }
static resolve(val) { return new TPromise((resolve, reject) => { if (val instanceof TPromise) { val.then(resolve, reject); } else { resolve(val); } }); }
static reject(val) { return new TPromise((resolve, reject) => { reject(val); }); } }
|