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
| Promise3.prototype.then = function (onFulfilled, onRejected) { onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : value => value; onRejected = typeof onRejected === 'function' ? onRejected : err => { throw err }; const self = this; let promise2; if (self.status === 'resolved') { promise2 = new Promise3(function(resolve, reject) { setTimeout(function () { try { let x = onFulfilled(self.value); resolvePromise(promise2, x, resolve, reject); } catch (e) { reject(e); } }); }); } if (self.status === 'rejected') { promise2 = new Promise3(function(resolve, reject) { setTimeout(function () { try { let x = onRejected(self.reason); resolvePromise(promise2, x, resolve, reject); } catch (e) { reject(e); } }); }); } if (self.status === 'pending') { promise2 = new Promise3(function(resolve, reject) { self.onResolvedCallbacks.push(function () { setTimeout(function() { try { let x = onFulfilled(self.value); resolvePromise(promise2, x, resolve, reject); } catch (e) { onRejected(e); } }); }); self.onRejectedCallbacks.push(function () { setTimeout(function() { try { let x = onRejected(self.reason); resolvePromise(promise2, x, resolve, reject); } catch (e) { onRejected(e); } }); }); }); } return promise2; }
|