在須要多個操做的時候,會致使多個回調函數嵌套,致使代碼不夠直觀,就是常說的回調地獄,一般經過promise來解決Promise本意是承諾,在程序中的意思就是承諾我過一段時間後會給你一個結果。 何時會用到過一段時間?答案是異步操做,異步是指可能比較長時間纔有結果的才作,例如網絡請求、讀取本地文件等數組
構造函數初始化邏輯
const PENDING = 'pending';//初始態 const FULFILLED = 'fulfilled';//初始態 const REJECTED = 'rejected';//初始態 let self = this;//先緩存當前promise實例 self.status = PENDING;//設置狀態 self.onResolvedCallbacks = [];//定義存放成功的回調的數組 self.onRejectedCallbacks = []; //定義存放失敗回調的數組
executor執行器,包含兩個參數,分別是resolve 解決和reject 拒絕,new Promise這個executor就會執行Promise有三個狀態:初始化狀態爲pending,成功狀態爲fulfilled,失敗狀態rejected,若是代碼一旦成功就不會走向失敗,若 一直pending 永遠不給你明確的答覆promise
當調用如下方法的時候,若是promise狀態爲pending的話能夠轉成成功態,若是已是成功態或者失敗態了,則什麼都不作緩存
function resolve(value){ if(value!=null &&value.then&&typeof value.then == 'function'){ return value.then(resolve,reject); } setTimeout(function(){ if(self.status == PENDING){ self.status = FULFILLED; self.value = value; self.onResolvedCallbacks.forEach(cb=>cb(self.value)); } }) } function reject(reason){ //2.1.2 setTimeout(function(){ if(self.status == PENDING){ self.status = REJECTED; self.value = reason; self.onRejectedCallbacks.forEach(cb=>cb(self.value)); } }); } }
由於此函數執行可能會異常,因此須要捕獲,若是出錯了,須要用錯誤對象reject,若是這函數執行失敗了,則用失敗的緣由reject這個promise,須要用try...catch(e)...進行處理
try{ executor(resolve,reject); }catch(e){ reject(e); };
Promise的解析過程
function resolvePromise(promise2,x,resolve,reject){ if(promise2 === x){ return reject(new TypeError('循環引用')); } let called = false; if(x instanceof Promise){ if(x.status == PENDING){ x.then(function(y){ resolvePromise(promise2,y,resolve,reject); },reject); }else{ x.then(resolve,reject); } }else if(x!= null &&((typeof x=='object')||(typeof x == 'function'))){ try{ let then = x.then; if(typeof then == 'function'){ then.call(x,function(y){ if(called)return; called = true; resolvePromise(promise2,y,resolve,reject) },function(err){ if(called)return; called = true; reject(err); }); }else{ resolve(x); } }catch(e){ if(called)return; called = true; reject(e); } }else{ resolve(x); } }
then
方法就是用來指定Promise 對象的狀態改變時肯定執行的操做,resolve 時執行第一個函數(onFulfilled),reject 時執行第二個函數(onRejected)
此方法中,若是成功和失敗的回調沒有傳,則表示這個then沒有任何邏輯,只會把值日後拋
Promise.prototype.then = function(onFulfilled,onRejected){ onFulfilled = typeof onFulfilled == 'function'?onFulfilled:function(value){return value}; onRejected = typeof onRejected == 'function'?onRejected:reason=>{throw reason}; let self = this; let promise2; if(self.status == FULFILLED){ return promise2 = new Promise(function(resolve,reject){ setTimeout(function(){ try{ let x =onFulfilled(self.value); resolvePromise(promise2,x,resolve,reject); }catch(e){ reject(e); } }) }); } if(self.status == REJECTED){ return promise2 = new Promise(function(resolve,reject){ setTimeout(function(){ try{ let x =onRejected(self.value); resolvePromise(promise2,x,resolve,reject); }catch(e){ reject(e); } }) }); } if(self.status == PENDING){ return promise2 = new Promise(function(resolve,reject){ self.onResolvedCallbacks.push(function(){ try{ let x =onFulfilled(self.value); resolvePromise(promise2,x,resolve,reject); }catch(e){ reject(e); } }); self.onRejectedCallbacks.push(function(){ try{ let x =onRejected(self.value); resolvePromise(promise2,x,resolve,reject); }catch(e){ reject(e); } }); }); } }
promise的鏈式調用
then
可使用鏈式調用的寫法緣由在於,每一次執行該方法時老是會返回一個Promise
對象catch
只是 promise.then(undefined, onRejected); 方法的一個別名而已。 也就是說,這個方法用來註冊當promise對象狀態變爲Rejected時的回調函數
catch原理就是隻傳失敗的回調
Promise.prototype.catch = function(onRejected){ this.then(null,onRejected); }
Promise.all
接收一個 promise對象的數組做爲參數,當這個數組裏的全部promise對象所有變爲resolve或reject狀態的時候,它纔會去調用 .then 方法
Promise.all = function(promises){ return new Promise(function(resolve,reject){ let done = gen(promises.length,resolve); for(let i=0;i<promises.length;i++){ promises[i].then(function(data){ done(i,data); },reject); } }); }
Promise.race
只要有一個promise對象進入 FulFilled 或者 Rejected 狀態的話,就會繼續進行後面的處理
Promise.race = function(promises){ return new Promise(function(resolve,reject){ for(let i=0;i<promises.length;i++){ promises[i].then(resolve,reject); } }); }
別人提供 給你一個方法,須要你傳入一個promise,但你只有一個普通的值,你就能夠經過這個方法把這個普通的值(string number object)轉成一個promise對象
返回一個馬上成功的promise
Promise.resolve = function(value){ return new Promise(function(resolve){ resolve(value); }); }
返回一個馬上失敗的promise
Promise.reject = function(reason){ return new Promise(function(resolve,reject){ reject(reason); }); }