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
| function MyPromise(fn) { let state = 'pending' let value = null const callbacks = []
function resolve(newValue) { const fn = () => { if (state !== 'pending') return
if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) { const { then } = newValue if (typeof then === 'function') { then.call(newValue, resolve, reject) return } } state = 'fulfilled' value = newValue handelCb() }
setTimeout(fn, 0) } function reject(error) { const fn = () => { if (state !== 'pending') return
if (error && (typeof error === 'object' || typeof error === 'function')) { const { then } = error if (typeof then === 'function') { then.call(error, resolve, reject) return } } state = 'rejected' value = error handelCb() } setTimeout(fn, 0) } function handelCb() { while (callbacks.length) { const fn = callbacks.shift() handle(fn) } }
function handle(callback) { if (state === 'pending') { callbacks.push(callback) return }
const cb = state === 'fulfilled' ? callback.onFulfilled : callback.onRejected const next = state === 'fulfilled' ? callback.resolve : callback.reject
if (!cb) { next(value) return } let ret; try { ret = cb(value) } catch (e) { callback.reject(e) } callback.resolve(ret); }
this.then = function (onFulfilled, onRejected) { return new MyPromise((resolve, reject) => { handle({ onFulfilled, onRejected, resolve, reject, }) }) }
this.catch = function (onError) { return this.then(null, onError) }
this.finally = function (onDone) { this.then(onDone, onError) } try { fn(resolve, reject) } catch(ex) { reject(ex); } }
MyPromise.resolve = function (value) { if (value && value instanceof MyPromise) { return value } if (value && typeof value === 'object' && typeof value.then === 'function') { const { then } = value return new MyPromise((resolve) => { then(resolve) }) } if (value) { return new MyPromise(resolve => resolve(value)) } return new MyPromise(resolve => resolve()) }
this.reject = function (value) { return new MyPromise(((resolve, reject) => { reject(value) })) }
MyPromise.all = function (arr) { const args = Array.prototype.slice.call(arr) return new MyPromise(((resolve, reject) => { if (args.length === 0) return resolve([]) let remaining = args.length
function res(i, val) { try { if (val && (typeof val === 'object' || typeof val === 'function')) { const { then } = val if (typeof then === 'function') { then.call(val, (val) => { res(i, val) }, reject) return } } args[i] = val if (--remaining === 0) { resolve(args) } } catch (ex) { reject(ex) } } for (let i = 0; i < args.length; i++) { res(i, args[i]) } })) }
MyPromise.race = function (values) { return new MyPromise(((resolve, reject) => { for (let i = 0, len = values.length; i < len; i++) { values[i].then(resolve, reject) } })) }
|