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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
| const PENDING = 'PENDING', FULFILLED = 'FULFILLED', REJECTED = 'REJECTED';
class MyPromise { constructor(executor) { this.status = PENDING; this.value = undefined; this.reason = undefined;
this.onFulfilledCallbacks = []; this.onRejectedCallbacks = [];
const resolve = (value) => { if (value instanceof MyPromise) { value.then(resolve, reject); return; }
if (this.status === PENDING) { this.status = FULFILLED; this.value = value;
this.onFulfilledCallbacks.forEach(fn => fn()); } };
const reject = (reason) => { if (this.status === PENDING) { this.status = REJECTED; this.reason = reason; this.onRejectedCallbacks.forEach(fn => fn()); } };
try { executor(resolve, reject); } catch (err) { reject(err); } }
then(onFulfilled, onRejected) { onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : value => value; onRejected = typeof onRejected === 'function' ? onRejected : reason => { throw reason }; let promise2 = new MyPromise((resolve, reject) => { if (this.status === FULFILLED) { setTimeout(() => { try { let x = onFulfilled(this.value); resolvePromise(promise2, x, resolve, reject); } catch (err) { reject(err); } }, 0); } if (this.status === REJECTED) { setTimeout(() => { try { let x = onRejected(this.reason); resolvePromise(promise2, x, resolve, reject); } catch (err) { reject(err); } }, 0); } if (this.status === PENDING) { this.onFulfilledCallbacks.push(() => { setTimeout(() => { try { let x = onFulfilled(this.value); resolvePromise(promise2, x, resolve, reject); } catch (err) { reject(err); } }, 0); }); this.onRejectedCallbacks.push(() => { setTimeout(() => { try { let x = onRejected(this.reason); resolvePromise(promise2, x, resolve, reject); } catch (err) { reject(err); } }, 0); }); } }); return promise2; } catch(errorCallback) { return this.then(null, errorCallback); }
finally(finallyCallback) { return this.then(value => { return MyPromise.resolve(finallyCallback()).then(() => value); }, reason => { return MyPromise.resolve(finallyCallback()).then(() => { throw reason; }); }); }
static resolve(value) { if (value instanceof MyPromise) { return value; }
return new MyPromise((resolve, reject) => { resolve(value); }); }
static reject(reason) { return new MyPromise((resolve, reject) => { reject(reason); }); }
static all(promiseArr) { let resArr = [], count = 0;
return new MyPromise((resolve, reject) => { promiseArr.map((promise, index) => { if (isPromise(promise)) { promise.then(value => { formatResArr(value, index, resolve); }, reject); } else { formatResArr(promise, index, resolve); } }); });
function formatResArr(value, index, resolve) { resArr[index] = value;
if(++count === promiseArr.length) { resolve(resArr); } } }
static allSettled(promiseArr) { if (!isIterator(promiseArr)) { throw new TypeError(promiseArr + ' is not iterable (cannot read property Symbol(Symbol.iterator))'); } let resArr = [], count = 0;
return new MyPromise((resolve, reject) => { if (promiseArr.length === 0) { resolve([]); } else { promiseArr.map((promise, index) => { if (isPromise(promise)) { promise.then(value => { formatResArr('fulfilled', value, index, resolve); }, reason => { formatResArr('rejected', reason, index, resolve); }); } else { formatResArr('fulfilled', promise, index, resolve); } }); } });
function formatResArr(status, value, index, resolve) { switch (status) { case 'fulfilled': resArr[index] = { status, value } break; case 'rejected': resArr[index] = { status, reason: value } break; default: break; } if (++count === promiseArr.length) { resolve(resArr); } } }
static race(promiseArr) { return new MyPromise((resolve, reject) => { if (promiseArr.length === 0) { resolve(); } else { promiseArr.map(promise => { MyPromise.resolve(promise).then(resolve, reject); }); } }); } }
function isPromise(x) { if ((typeof x === 'object' && x !== null) || typeof x === 'function') { let then = x.then; return typeof then === 'function'; } return false; }
function isIterator(value) { return value !== undefined && value !== null && typeof value[Symbol.iterator] === 'function'; }
function resolvePromise(promise2, x, resolve, reject) { if (promise2 === x) { reject(new TypeError('Chaining cycle detected for promise #<MyPromise>')); } let called = false; if ((typeof x === 'object' && x !== null) || typeof x === 'function') { try { let then = x.then;
if (typeof then === 'function') { then.call(x, y => { if (called) return; called = true; resolvePromise(promise2, y, resolve, reject); }, r => { if (called) return; called = true; reject(r); }); } else { resolve(x); } } catch(err) { if (called) return; called = true; reject(err); } } else { resolve(x); } }
MyPromise.defer = MyPromise.deferred = function() { let deferred = {}; deferred.promise = new MyPromise((resolve, reject) => { deferred.resolve = resolve; deferred.reject = reject; }); return deferred; }
module.exports = MyPromise;
|