摘要:緩存默認狀態(tài)為此變量里放著此的結(jié)果存放的著所有成功的回調(diào)函數(shù)存放著所有的失敗的回調(diào)函數(shù)調(diào)用此方法可以把變成成功態(tài)的時候你把掙到的錢傳進來調(diào)用此方法可以把當(dāng)前的變成失敗態(tài)如果當(dāng)前狀態(tài)是初始態(tài),則轉(zhuǎn)成失敗態(tài)立即執(zhí)行傳入的任務(wù)如果就是循環(huán)引用成功
function Promise(task) { let that = this;//緩存this //默認狀態(tài)為pending that.status = "pending"; //此變量里放著此promise的結(jié)果 that.value = undefined; //存放的著所有成功的回調(diào)函數(shù) that.onResolvedCallbacks = []; //存放著所有的失敗的回調(diào)函數(shù) that.onRejectedCallbacks = []; //調(diào)用此方法可以把promise變成成功態(tài) //resolve的時候你把掙到的錢傳進來 function resolve(value) { if(value instanceof Promise){ return value.then(resolve,reject); } if (that.status == "pending") { that.status = "fulfilled"; that.value = value; that.onResolvedCallbacks.forEach(item=>item(that.value)); } } //調(diào)用此方法可以把當(dāng)前的promise變成失敗態(tài) function reject(reason) { //如果當(dāng)前狀態(tài)是初始態(tài),則轉(zhuǎn)成失敗態(tài) if (that.status == "pending") { that.status = "rejected"; that.value = reason; that.onRejectedCallbacks.forEach(item=>item(that.value)); } } //立即執(zhí)行傳入的任務(wù) try { task(resolve, reject); } catch (e) { reject(e); } } function resolvePromise(promise2,x,resolve,reject){ let then; //如果x就是promise2 if(promise2 === x){ return reject(new TypeError("循環(huán)引用")); } if(x instanceof Promise){ if(x.status == "pending"){ x.then(function(y){ resolvePromise(promise2,y,resolve,reject); },reject); }else if(x.status == "fulfilled"){ resolve(x.value); }else if(x.status == "rejected"){ reject(x.value); } }else if(x!=null && (typeof x == "object" || typeof x == "function")){ try{ then = x.then; if(typeof then == "function"){ then.call(x,function(y){ resolvePromise(promise2,y,resolve,reject) },reject); } }catch(e){ reject(e); }; }else{ resolve(x); } } //onFulfilled成功的回調(diào),onReject失敗的回調(diào) Promise.prototype.then = function (onFulfilled, onReject) { onFulfilled = typeof onFulfilled == "function"?onFulfilled:function(value){return value}; onReject = typeof onReject=="function"?onReject:function(reason){ throw reason; } let that = this; let promise2; if(that.status == "fulfilled"){ promise2 = new Promise(function(resolve,reject){ let x = onFulfilled(that.value); resolvePromise(promise2,x,resolve,reject); }); } if(that.status == "rejected"){ promise2 = new Promise(function(resolve,reject){ let x = onReject(that.value); resolvePromise(promise2,x,resolve,reject); }); } if(that.status == "pending"){ promise2 = new Promise(function(resolve,reject){ that.onResolvedCallbacks.push(function(){ let x = onFulfilled(that.value); resolvePromise(promise2,x,resolve,reject); }); that.onRejectedCallbacks.push(function(){ let x = onReject(that.value); resolvePromise(promise2,x,resolve,reject); }); }); } return promise2; } Promise.all = function (arr) { return new Promise((resolve,reject) =>{ let values = [] let len = arr.length for(var i = 0;i < len; i++){ let promise = arr[i] if(typeof promise.then == "function"){ promise.then(res=>{ values.push(res) if(values.length == len){ resolve(values) } }) } } }) } Promise.race = function (arr) { return new Promise((resolve,reject) =>{ let len = arr.length for(var i = 0;i < len; i++){ let promise = arr[i] if(typeof promise.then == "function"){ promise.then(res=>{ resolve(res) }) } } }) } Promise.resolve = function (value) { if (value instanceof Promise) return value return new Promise(resolve => resolve(value)) } Promise.reject = function (value) { return new Promise((resolve,reject)=>reject(value)) } module.exports = Promise;
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/92418.html
摘要:手寫一款符合規(guī)范的長篇預(yù)警有點長,可以選擇性觀看。初始狀態(tài)是,狀態(tài)可以有或者不能從轉(zhuǎn)換為或者從轉(zhuǎn)換成即只要由狀態(tài)轉(zhuǎn)換為其他狀態(tài)后,狀態(tài)就不可變更。 手寫一款符合Promise/A+規(guī)范的Promise 長篇預(yù)警!有點長,可以選擇性觀看。如果對Promise源碼不是很清楚,還是推薦從頭看,相信你認真從頭看到尾,并且去實際操作了,肯定會有收獲的。主要是代碼部分有點多,不過好多都是重復(fù)的,不...
摘要:有三種狀態(tài),等待中,已完成,已失敗。對象狀態(tài)不受外界影響,只有異步操作的結(jié)果可以改變狀態(tài),這就是的由來怎么用接受一個函數(shù)作為參數(shù),這個參數(shù)函數(shù)的兩個參數(shù)分別是和,用來執(zhí)行了兩種狀態(tài)的回調(diào)函數(shù)。當(dāng)實例生成后,用方法來指定兩種狀態(tài)的回調(diào)函數(shù)。 什么是Promise 官方的解釋:Promise是一個用來傳遞異步操作消息的對象。Promise有三種狀態(tài),pending(等待中),resolve...
摘要:極簡版滿足的使用方式生成實例對象的方式通過類直接調(diào)用靜態(tài)方法,目前靜態(tài)方法僅支持親測使用,歡迎指教,互相學(xué)習(xí),鏈接,歡迎。附贈利用構(gòu)造函數(shù)手寫的方法,鏈接。 極簡版Promise 滿足的使用方式 生成實例對象的方式:new MyPromise() 通過類直接調(diào)用靜態(tài)方法:MyPromise.resolve(),目前靜態(tài)方法僅支持resolve & reject 親測使用OK,歡...
摘要:簡易版本的第一步列出三大塊第二步負責(zé)注冊所有的函數(shù)負責(zé)執(zhí)行所有的函數(shù)第三步在里面要加上防止還沒進行注冊就直接執(zhí)行了第四步里面要返回這樣就可以鏈?zhǔn)秸{(diào)用了第五步三個狀態(tài)的管理的鏈?zhǔn)秸{(diào)用在里面一個這樣才能里面加上異步函數(shù)加上了加入狀態(tài)為了解決在異 // 簡易版本的promise // 第一步: 列出三大塊 this.then resolve/reject fn(resolv...
閱讀 3497·2023-04-26 02:00
閱讀 3087·2021-11-22 13:54
閱讀 1705·2021-08-03 14:03
閱讀 717·2019-08-30 15:52
閱讀 3094·2019-08-29 12:30
閱讀 2426·2019-08-26 13:35
閱讀 3371·2019-08-26 13:25
閱讀 3007·2019-08-26 11:39