首页 > 试题广场 >

promise实现

const PENDING = 'PENDING'
const FULFILLED = 'FULFILLED'
const REJECT = 'REJECT'
class Promise {
    constructor(exector) {
        this.state = 'PENDING'
        this.successData = undefined
        this.errorData = undefined
        this.resolveList = []
        this.rejectList = []
        const resolve = (value) =>{
            if(this.state == 'PENDING'){
                this.state = 'FULFILLED'
                this.successData = value
                this.resolveList.forEach(fn => fn())
            }
        }
        const reject = (value) =>{
            if(this.state == 'PENDING'){
                this.state = 'REJECT'
                this.errorData = value
                this.rejectList.forEach(fn => fn())
            }
        }
        try {
            exector(resolve, reject)
        } catch (error) {
            reject(error)
        }
    }
    then(resolveFn, rejectFn){
        if(this.state == 'FULFILLED') {
            resolveFn(this.successData)
        }
        if(this.state == 'REJECT') {
            rejectFn(this.errorData)
        }
        if(this.state == 'PENDING') {
            this.resolveList.push(()=>{resolveFn(this.successData)})
            this.rejectList.push(()=>{rejectFn(this.errorData)})
        }
    }
}
发表于 2022-03-21 21:45:06 回复(0)
<script type="text/javascript">
    function myPromise(constructor){
        let self = this;
        self.status = "pending"  //定义状态改变时的初始状态
        self.value = undefined;  //定义状态为resolve时的值
        self.reason = undefined;  //定义状态为rejected的值
        function resolve(value) {
            if(self.status === "pending"){
                self.value = value;
                self.status = "resolved"
            }
        }
        function reject(reason){
            if(self.status === "pending"){
                self.reason = reason;
                self.status = "rejected"
            }
        }
        //捕获构造异常
        try{
            constructor(resolve,reject);
        }catch(e) {
            reject(e)
        }
    }

    //同时需要在myPromise的原型上定义链式调用的then方法
    myPromise.prototype.then = (onResolved, onRejected) => {
        let self = this;
        switch(self.status){
            case "resolved":{
                onResolved(self.value);
                break;
            }
            case "rejected":{
                onRejected(self.reason);
                break
            }
            default;
        }
    }
</script>


发表于 2022-03-20 16:27:21 回复(0)
const PENDING='pengding'
const FULFILLED='fulfilled'
const REJECTED='rejected'
class MyPromise{
  constructor(executor){
    executor(this.resolve,this.reject)
  }
  status=PENDING
  value=null
  resaon=null
  resolve=(value)=>{
    if(this.status==PENDING){
      this.status=FULFILLED
      this.value=value
    }
  }
  reject=(resaon)=>{
    if(this.status==PENDING){
      this.status=REJECTED
      this.resaon=resaon
    }
  }
  then(onFulfilled,onRejected){
    if(this.status==FULFILLED){
      onFulfilled(this.value)
    }else if(this.status==REJECTED){
      onRejected(this.reason)
    }
  }
}
发表于 2022-03-03 09:45:52 回复(0)