咱们先看看代码
首先咱们先来回顾一下Promise的用法前端
new Promise((resolve,reject)=>{ resolve('正确的') }).then(value =>{ console.log(value); })
Promise
的用法并不难,咱们只要知道 new Promise
给Promise
传入function(resolve,reject)
,接着在函数内作须要作的事情便可,等到须要的时候,对这个构造函数.then(res=>{})
便可获取对应的值(function (window) { function MyPromise(executor) { // 1 定义MyPromise 构造函数 function resolve(value) { // 定义resolve } function reject(reason) { // 定义reject } MyPromise.prototype.then = function (onResolved,onRejected) { // 定义then } MyPromise.prototype.catch = function (error) { // 定义catch } // 定义执行器的执行 executor(resolve,reject); } window.MyPromise = MyPromise; // 2导出 })(window)
(function (window) { // Promise function Mypromise(executor) { const self = this; // 须要定义一下this指向 self.status = 'pennding'; // 初始化状态 self.data = undefined; // promise对象指定一个用于存储结果数据 self.callbacks = []; // 每一个元素的结构 { onResolved(){}, onRejected(){}} function resolve(value) { // resolve if (self.status !== 'pennding') { // 由于promise 状态只能修改一次 return; } self.status = 'resolve'; // 改变为 resolve self.data = value; // 保存value的值 if (self.callbacks.length > 0) { // 若是有待执行的callback函数,当即执行回调函数onResolved setTimeout(() => { // 当前方案是为了将任务挂在队列中,制造异步 self.callbacks.forEach(callbacksObj => { callbacksObj.onResolved(value) }) },0) } } function reject(reason) { //reject if (self.status !== 'pennding') { // 由于promise 状态只能修改一次 return; } self.status = 'reject'; // 改变为 reject self.data = reason; // 保存reason的值 if (self.callbacks.length > 0) { // 若是有待执行的callback函数,当即执行回调函数onRejected setTimeout(() => { self.callbacks.forEach(callbacksObj => { callbacksObj.onRejected(reason) }) },0) } } try { // 若是执行器抛出异常 executor(resolve, reject); } catch (error) { reject(error) } } // Promise.then() Mypromise.prototype.then = function (onResolved, onRejected) { // 假设当前状态仍是pennding const self = this; self.callbacks.push({ onResolved, onRejected }) } // Promise.catch() Mypromise.prototype.carch = function (error) { } window.Mypromise = Mypromise; })(window);
<body> <script src="./promise.js"></script> <script> const p = new Mypromise((resolve, reject) => { setTimeout(() => { // 由于拆分 then还未处理,须要p.then 先执行 resolve(1) console.log("我先") }, 100) }) p.then(value => { console.log("onResolve()1", value) }, reason => { console.l("onReject()1", reason) }) p.then(value => { console.log("onResolve()2", value) }, reason => { console.l("onReject()2", reason) }) </script> </body>
因此这个接受的参数叫作执行器(executor)promise
resolve,reject
改变状态,改变值<body> <script src="./promise1.js"></script> <script> const p = new MyPromise((resolve, reject) => { setTimeout(() => { // 由于拆分缘由then 还未作对应处理,此时不能改变状态 resolve(1) console.log("我先") }, 100) }) p.then(value => { console.log("onResolve()1", value) }, reason => { console.l("onReject()1", reason) }) p.then(value => { console.log("onResolve()2", value) }, reason => { console.l("onReject()2", reason) }) </script> </body>
(function (window) { const PENDDING = 'pendding'; const FULFILLED = 'fulfilled'; const REJECTED = 'rejected'; function MyPromise(executor) { // 定义MyPromises构造函数 const self = this; self.status = PENDDING; self.data = undefined; self.callbacks = []; function resolve(value) { if (self.status === PENDDING) { self.status = FULFILLED; // 改变MyPromise状态 self.data = value; // MyPromise 的值对应变化 setTimeout(() => { // 异步执行 self.callbacks.forEach(callbacksObj => { // 若是有待执行的callback函数,当即执行回调 callbacksObj.onResolved(value) }) }) } } function reject(reason) { if (self.status === PENDDING) { self.status = REJECTED; self.data = reason; setTimeout(() => { self.callbacks.forEach(callbacksObj => { callbacksObj.onRejected(reason); }); }) } } try { // MyPromise能够抛出异常 executor(resolve, reject); } catch (error) { reject(error) } } /* MyPromise原型对象的then() 指定成功和失败回调 返回一个新的回调函数 // 返回的MyPromise结果由onResolved/onRejected的结果决定 */ MyPromise.prototype.then = function (onResolved, onRejected) { // 定义then const self = this; // 指定回调函数的默认值(必须是函数) onResolved = typeof onResolved==='function' ? onResolved : value => value; onRejected = typeof onRejected==='function' ? onRejected : reason => {throw reason}; return new MyPromise((resolve,reject)=>{ // 返回一个新的MyPromise对象 function handle(callback) { // 返回的MyPromise结果由onResolved/onRejected的结果决定 // 一、抛出异常MyPromise结果为失败 reason为结果 // 二、返回的是MyPromise MyPromise为当前的结果 // 三、返回的不是MyPromise value为结果 // 须要经过捕获获取才能知道有没有异常 try{ const result = callback(self.data) // 判断是否是MyPromise if ( result instanceof MyPromise){ // 只有then才知道结果 result.then(value=>resolve(value),reason=>reject(reason)) }else{ resolve(result) } }catch(error){ reject(error) // 返回的结果为reject(error) 上面第一点 } } // 判断当前的status if (self.status === FULFILLED){ // 状态为 fulfilled setTimeout(()=>{ // 当即执行异步回调 handle(onResolved); }) } else if (self.status === REJECTED){ // 状态为 rejected setTimeout(()=>{ // 当即执行异步回调 handle(onRejected); }) }else{ // pendding将成功和失败保存到callbacks里缓存起来 self.callbacks.push({ onResolved(value){ //函数里面调用回调函数 而且根据回调函数的结果改变MyPromise的结果 handle(onResolved) //为何这里没有setTimeout,由于上面已经写到过改变状态后回去callbacks里循环待执行的回调函数 }, onRejected(reason){ handle(onRejected) } }) } }) } MyPromise.prototype.catch = function (onRejected) { //定义then return this.then(undefined,onRejected) } window.MyPromise = MyPromise; // 导出MyPromise })(window)
catch