promise实现

const isFunction = variable => typeof variable =‘function’
// 定义Promise的三种状态常量 const PENDING = ‘PENDING’ const FULFILLED = ‘FULFILLED’ const REJECTED = ‘REJECTED’//构造函数Promise必须接受一个函数handle作为参数,handle又包含resolve和reject两个参数,它们是两个函数。class Promise { constructor (handle) { if (!isFunction(handle)) { throw new Error(‘MyPromise must accept a function as a parameter’) } //状态 this._state=pending //状态发生改变时传递给回调函数的值 this._value=undefined //成功回调队列 this._fulfilledQueues=[] //失败回调队列 this._rejectedQueues=[] try{ handle(this._resolve.bind(this), this._rejected.bind(this)) }catch(err){ this._rejected(err) } } //把state 从pending改成fulFilled _resolve(val){ const run=()=>{ if (this._state!PENDING) return this._state=FULFILLED //依次执行_fulfilledQueues队列里的回调,并清空 const runFulfiled=value=>{ while(cb=this._fulfilledQueues){ cb(value) } } //依次执行_rejectedQueues队列里的回调,并清空 const runRejected=err=>{ while(cb=this._rejectedQueues){ cb(err) } } //如果resolve的参数是Promise,则必须等待参数promise的状态改变后,当前promise的状态才会改变,且取决于参数Promise的状态 if(val instanceOf Promise){ val.then(value=>{ this._value=value runFulfiled(value) },err=>{ this._value=err runRejected(err) } }) }else{ this._value=val runFulfiled(val) }//为了支持支持同步Promise 异步调用 setTimeout(run,0)}//把状态从pending转化为rejected,并依次执行失败回调,清空_rejectedQueues_reject (err) { if (this._status ! PENDING) return // 依次执行失败队列中的函数,并清空队列 const run = () => { this._status = REJECTED this._value = err let cb; while (cb = this._rejectedQueues.shift()) { cb(err) } } // 为了支持同步的Promise,这里采用异步调用 setTimeout(run, 0) } then(onFulfilled,onRejected){ const { _state,_value}=this return new Promise((onFulfilledNext, onRejectedNext)=>{ let fulfilled=value=>{ try{ if(!isFunction(onFulfilled)){ onFulfilledNext(value) }else{ let res = onFulfilled(value) if(res instanceof Promise){ res.then(onFulfilledNext, onRejectedNext) }else{ onFulfilledNext(res) } } }catch(err){ onRejectedNext(err) } }, let rejected = error => { try { if (!isFunction(onRejected)) { onRejectedNext(error) } else { let res = onRejected(error); if (res instanceof MyPromise) { // 如果当前回调函数返回MyPromise对象,必须等待其状态改变后在执行下一个回调 res.then(onFulfilledNext, onRejectedNext) } else { //否则会将返回结果直接作为参数,传入下一个then的回调函数,并立即执行下一个then的回调函数 onFulfilledNext(res) } } } catch (err) { // 如果函数执行出错,新的Promise对象的状态为失败 onRejectedNext(err) } } switch(_state){ case PENDING this._fulfilledQueues.push(fulfilled) this._rejectedQueues.push(rejected) break case FULFILLED fulfilled(_value) break case REJECTED rejected(_value) break } })//Promise结尾 }//then结尾catch (onRejected) { return this.then(undefined, onRejected)}// 添加静态resolve方法 static resolve (value) { // 如果参数是MyPromise实例,直接返回这个实例 if (value instanceof MyPromise) return value return new MyPromise(resolve => resolve(value)) } static reject (value) { return new MyPromise((resolve ,reject) => reject(value)) } static all(list){ return new Promise((resolve,reject)=>{ let values=[] let count=0 for(let [i,p] of list.entries()){ this.resolve§.then(res=>{ values[i]=res count++ //list所有状态都变成FULFILLED,返回的Promise状态才变成FULFILLED if(count
=values.length) resolve(values) },err=>{ reject(err) } } } static race (list) { return new MyPromise((resolve, reject) => { for (let p of list) { // 只要有一个实例率先改变状态,新的MyPromise的状态就跟着改变 this.resolve§.then(res => { resolve(res) }, err => { reject(err) }) } }) }finally (cb) { return this.then( value => MyPromise.resolve(cb()).then(() => value), reason => MyPromise.resolve(cb()).then(() => { throw reason }) }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值