0%
特点
- 三种状态:分别是等待中(pending), 完成了(resolved),拒绝了(rejected)
- 状态一旦从等待中变成其他状态就永远不能更改状态
- 状态一旦改变不可取消
实现一个简易版 Promise
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
| const PENDING = 'pending' const RESOLVED = 'resolved' const REJECTED = 'rejected'
function MyPromise(fn){ const that = this that.state = PENDING that.value = null that.resolvedCallbacks = [] that.rejectedCallbacks = []
function resolve(value){ if(that.state === PENDING){ that.state = RESOLVED that.value = value that.resolvedCallbacks.map(cb => cb(that.value)) } }
function reject(value){ if(that.state === PENDING){ that.state = REJECTED that.value = value that.rejectedCallbacks.map(cb => cb(that.value)) } }
try{ fn(resolve, reject) }catch(e){ reject(e) } }
MyPromise.prototype.then = function(onFulfilled, onRejected){ const that = this onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : v => v onRejected = typeof onFulfilled === 'function' ? onRejected : err => { throw err }
if(that.state === PENDING){ that.resolvedCallbacks.push(onFulfilled) that.rejectedCallbacks.push(onRejected) }
if(that.state === RESOLVED){ onFulfilled(that.value) }
if(that.state === REJECTED){ onRejected(that.value) } }
new MyPromise((resolve, reject) => { setTimeout(() => { resolve(1) }, 0) }).then(value => { console.log(value) })
|