//promise:then,catch,finally,all,race // then时state还是pending等待状态 我们就需要在then调用的时候,将成功和失败存到各自的数组,一旦reject或者resolve,就调用它们,类似发布订阅 //pending,fulfilled,rejected const pedding = 'pedding' const fulfilled = 'fulfilled' const rejected = 'rejected' class Promise { constructor(executor) { console.log('executor:'+ executor) this.status = pedding //初始状态 this.value = undefined //成功或者失败的值 this.fail = undefined this.successCallback = [] //成功或者失败的回调函数 this.errorCallback = [] let resolve = (value) => { //成功之后就依次执行 setTimeout(() => { if (this.status === pedding) {//只有在状态是pending的时候才能改变 //console.log('resolve:'+value) this.status = fulfilled this.value = value this.successCallback.forEach(fn => { fn() }) } }, 0); }; let reject = (fail) => { setTimeout(() => { if (this.status === pedding) { // console.log('reject:'+ fail) this.status = rejected this.fail = fail this.errorCallback.forEach(fn => { fn() }) } }, 0); }; try { executor(resolve,reject) }catch(err){ reject(err) } } then(success,error) { if(this.status === fulfilled) { return new Promise((resolve,reject) => { try{ let fun = success(this.value) if(fun instanceof Promise) { fun.then(resolve,reject) }else{ resolve(fun) } }catch(err) { reject(err) } }) } if(this.status === rejected) { return new Promise((resolve, reject) => { try { let j = error(this.fail) if (j instanceof Promise) { j.then(resolve, reject) } else { resolve(j) } } catch (err) { reject(err) } }) } // console.log(this.reject) // 订阅成功失败 if(this.status === pedding) {//刚进来必须是pedding状态,否则状态一旦变了回调还没有注册 return new Promise((resolve, reject) => { this.successCallback.push(() => { let f = success(this.value) if (f instanceof Promise) { f.then(resolve, reject) } else { resolve(f) } }) this.errorCallback.push(() =>{ let j = error(this.fail) if (j instanceof Promise) { j.then(resolve, reject) } else { resolve(j) } }) }) } } } let p = new Promise((resolve,reject) =>{ //reject('失败') resolve('成功1') }) p.then((res) => { console.log('then的第一个参数成功'+res) return new Promise((resolve,reject) =>{ //reject('失败') resolve('成功2') }) },()=>{ console.log('失败') }).then((res) => { console.log('then的第二个参数成功'+res) },()=>{ console.log('失败2') }) // .then((res) => { // console.log('then的第3个参数成功'+res) // },()=>{ // console.log('失败3') // }) // p.then() //直接在then后面then 报错Cannot read property 'then' of undefined,所以需要返回一个新的promise对象
输出 executor:(resolve,reject) =>{ //reject('失败') resolve('成功1') } executor:(resolve, reject) => { this.successCallback.push(() => { let f = success(this.value) if (f instanceof Promise) { f.then(resolve, reject) } else { resolve(f) } }) this.errorCallback.push(() =>{ let j = error(this.fail) if (j instanceof Promise) { j.then(resolve, reject) } else { resolve(j) } }) } executor:(resolve, reject) => { this.successCallback.push(() => { let f = success(this.value) if (f instanceof Promise) { f.then(resolve, reject) } else { resolve(f) } }) this.errorCallback.push(() =>{ let j = error(this.fail) if (j instanceof Promise) { j.then(resolve, reject) } else { resolve(j) } }) } then的第一个参数成功成功1 executor:(resolve,reject) =>{ //reject('失败') resolve('成功2') } executor:(resolve, reject) => { this.successCallback.push(() => { let f = success(this.value) if (f instanceof Promise) { f.then(resolve, reject) } else { resolve(f) } }) this.errorCallback.push(() =>{ let j = error(this.fail) if (j instanceof Promise) { j.then(resolve, reject) } else { resolve(j) } }) } then的第二个参数成功成功2 [Done] exited with code=0 in 0.095 seconds