- 步骤
- 1、 搭框架实现同步改变内部状态的三种情况 resolve(1) reject(2) throw ‘Error’ 只改变一次状态
- 2、 实现内部只能改变一次状态 .then resolveFun rejectFun 加入异步执行
- 3、_对then方法返回结果进行处理(是否返回promise) 返回一个promise 对then里面的promise处理状态改变, 对状态改变添加异步, 多次then的处理 优化_resolveFun, rejectFun缺省的情况
- 4、添加catch this.then(undefined,rejectFun)
- 9、添加Promise.resolve
- 10、添加Promise.reject
- 11、添加Promise.all
- 12、添加Promise.race
- 13、完整版
- 14、class
步骤
1、 搭框架实现同步改变内部状态的三种情况 resolve(1) reject(2) throw ‘Error’ 只改变一次状态
function Promise(excetor){let _this = this;_this.PromiseStatus = "pedding",_this.PromiseResult = null,_this.callbacks = [];function resolve(value){if(_this.PromiseStatus!=='pedding')return_this.PromiseStatus = 'fulfilled'_this.PromiseResult = value;}function reject(reason){if(_this.PromiseStatus!=='pedding')return_this.PromiseStatus = 'rejected'_this.PromiseResult = reason;}try{excetor(resolve,reject)}catch(error){reject(error)throw 'promise 内部错误' + error}}
2、 实现内部只能改变一次状态 .then resolveFun rejectFun 加入异步执行
_
function Promise(excetor){let _this = this,_this.PromiseStatus = "pedding",_this.PromiseResult = null,_this.callbacks = [];function resolve(value){if(_this.PromiseStatus!=='pedding')return_this.PromiseStatus = 'fulfilled'_this.PromiseResult = value;setTimeout(()=>{_this.callbacks.forEach(item=>{item.resolveFun()})})}function reject(reason){if(_this.PromiseStatus!=='pedding')return_this.PromiseStatus = 'rejected'_this.PromiseResult = reason;setTimeout(()=>{_this.callbacks.forEach(item=>{item.rejectFun()})})}try{excetor(resolve,reject)}catch(error){reject(error)throw 'promise 内部错误' + error}}Promise.prototype.then(resolveFun,rejectFun){if(_this.PromiseStatus === 'fulfilled'){resolveFun(this.PromiseResult)}if(_this.PromiseStatus === 'rejected'){rejectFun(this.PromiseResult)}if(_this.PromiseStatus === 'pedding'){this.callbacks.push({resolveFun,rejectFun})}}
3、_对then方法返回结果进行处理(是否返回promise) 返回一个promise 对then里面的promise处理状态改变, 对状态改变添加异步, 多次then的处理 优化_resolveFun, rejectFun缺省的情况
_
Promise.prototype.then = function(resolveFun,rejectFun){let _this = this;if(typeof resolveFun!=='function')resolveFun = value=>value;if(typeof rejectFun!=='function') rejectFun = reason => {throw reason};return new Promise((resolve,reject)=>{function dealStatus(type){try{let result = type(_this.PromiseResult)if(result instanceof Promise){result.then(v=>{resolve(v)},r=>{reject(r)})}else{resolve(result)}}catch(error){reject(error)}}if(_this.PromiseStatus === 'fulfilled'){setTimeout(()=>{dealStatus(resolveFun)})}if(_this.PromiseStatus === 'rejected'){setTimeout(()=>{dealStatus(rejectFun)})}if(_this.PromiseStatus === 'pedding'){this.callbacks.push({resolveFun:function(){dealStatus(resolveFun)},rejectFun:function(){dealStatus(rejectFun)}})}})}
4、添加catch this.then(undefined,rejectFun)
Promise.prototype.catch=function(rejectFun){return this.then(undefined,rejectFun)}
9、添加Promise.resolve
Promise.resolve = function(value){return new Promise((resolve,reject) => {if(value instanceOf Promise){value.then(v=>{resolve(v)},r=>{reject(r)})}else{resolve(value)}})}
10、添加Promise.reject
Promise.reject = function(reason){return new Promise((resolve,reject) => {reject(reason)})}
11、添加Promise.all
原理: 数组中多个Promise对象 , 依次执行,全部执行成功后,resolve, 否则reject
Promise.all = function(promises){return new Promise((resolve,reject)=>{let count, arr=[];for(let i=0;i<promises.length;i++){promises[i].then(v=>{count++arr[i] = vif(count === promises.length){resolve(arr)}},r=>{reject(r)})}})}
12、添加Promise.race
原理:返回一个promise实例。 在多个promise对象在循环处理中,根据最先得到的结果判断promise内部的状态,结果值为promise的值
Promise.race = function(promises){return new Promise((resolve,reject)=>{for(let i=0;i<promises.length;i++){promises[i].then(v=>{resolve(v)},r=>{reject(r)})}})}
13、完整版
function Promise(excetor){let _this = this;_this.PromiseStatus = "pedding",_this.PromiseResult = null,_this.callbacks = [];function resolve(value){if(_this.PromiseStatus!=='pedding')return_this.PromiseStatus = 'fulfilled'_this.PromiseResult = value;setTimeout(()=>{_this.callbacks.forEach(item=>{item.resolveFun()})})}function reject(reason){if(_this.PromiseStatus!=='pedding')return_this.PromiseStatus = 'rejected'_this.PromiseResult = reason;setTimeout(()=>{_this.callbacks.forEach(item=>{item.rejectFun()})})}try{excetor(resolve,reject)}catch(error){reject(error)throw 'promise 内部错误' + error}}Promise.prototype.then = function(resolveFun,rejectFun){let _this = this;if(typeof resolveFun!=='function')resolveFun = value=>value;if(typeof rejectFun!=='function') rejectFun = reason => { throw reason };return new Promise((resolve,reject)=>{function dealStatus(type){try{let result = type(_this.PromiseResult)if(result instanceof Promise){result.then(v=>{resolve(v)},r=>{reject(r)})}else{resolve(result)}}catch(error){reject(error)}}if(_this.PromiseStatus === 'fulfilled'){setTimeout(()=>{dealStatus(resolveFun)})}if(_this.PromiseStatus === 'rejected'){setTimeout(()=>{dealStatus(rejectFun)})}if(_this.PromiseStatus === 'pedding'){this.callbacks.push({resolveFun:function(){dealStatus(resolveFun)},rejectFun:function(){dealStatus(rejectFun)}})}})}Promise.prototype.catch=function(rejectFun){return this.then(undefined,rejectFun)}Promise.resolve = function(value){return new Promise((resolve,reject) => {if(value instanceof Promise){value.then(v=>{resolve(v)},r=>{reject(r)})}else{resolve(value)}})}Promise.reject = function(reason){return new Promise((resolve,reject) => {reject(reason)})}Promise.all = function(promises){let count, arr=[];return new Promise((resolve,reject)=>{for(let i=0;i<promises.length;i++){promises[i].then(v=>{count++arr[i] = vif(count === promises.length){resolve(arr)}},r=>{reject(r)})}})}Promise.race = function(promises){return new Promise((resolve,reject)=>{for(let i=0;i<promises.length;i++){promises[i].then(v=>{resolve(v)},r=>{reject(r)})}})}// 不管promise最后的状态,在执行完then或catch指定的回调函数以后,都会执行finally方法指定的回调函数。Promise.finally = function(fn) {return this.then((data) => {return Promise.resole(fn()).then(() => value)}, (err) => {return Promise.resole(fn()).then(() => { throw err })})}
14、class
export default class MPromise {constructor(excetor) {console.log('111111////')let _this = this;(_this.PromiseStatus = "pedding"),(_this.PromiseResult = null),(_this.callbacks = []);function resolve(value) {if (_this.PromiseStatus !== "pedding") return;_this.PromiseStatus = "fulfilled";_this.PromiseResult = value;setTimeout(() => {_this.callbacks.forEach((item) => {item.resolveFun();});});}function reject(reason) {if (_this.PromiseStatus !== "pedding") return;_this.PromiseStatus = "rejected";_this.PromiseResult = reason;setTimeout(() => {_this.callbacks.forEach((item) => {item.rejectFun();});});}try {excetor(resolve, reject);} catch (error) {reject(error);throw "promise 内部错误" + error;}}then(resolveFun, rejectFun) {let _this = this;if (typeof resolveFun !== "function") resolveFun = (value) => value;if (typeof rejectFun !== "function")rejectFun = (reason) => {throw reason;};return new Promise((resolve, reject) => {function dealStatus(type) {try {let result = type(_this.PromiseResult);if (result instanceof Promise) {result.then((v) => {resolve(v);},(r) => {reject(r);});} else {resolve(result);}} catch (error) {reject(error);}}if (_this.PromiseStatus === "fulfilled") {setTimeout(() => {dealStatus(resolveFun);});}if (_this.PromiseStatus === "rejected") {setTimeout(() => {dealStatus(rejectFun);});}if (_this.PromiseStatus === "pedding") {this.callbacks.push({resolveFun: function () {dealStatus(resolveFun);},rejectFun: function () {dealStatus(rejectFun);},});}});}catch(rejectFun) {return this.then(undefined, rejectFun);}static resolve(value) {return new Promise((resolve, reject) => {if (value instanceof Promise) {value.then((v) => {resolve(v);},(r) => {reject(r);});} else {resolve(value);}});}static reject(reason) {return new Promise((resolve, reject) => {reject(reason);});}static all(promises) {let count,arr = [];return new Promise((resolve, reject) => {for (let i = 0; i < promises.length; i++) {promises[i].then((v) => {count++;arr[i] = v;if (count === promises.length) {resolve(arr);}},(r) => {reject(r);});}});}static race(promises) {return new Promise((resolve, reject) => {for (let i = 0; i < promises.length; i++) {promises[i].then((v) => {resolve(v);},(r) => {reject(r);});}});}static finally(fn) {return this.then((value) => Promise.resolve(fn()).then((v) => {console.log(v,value)value}),(error) =>Promise.resolve(fn()).then((err) => {throw error;}));}}
