開發不少的時候須要異步操做,經常使用的作法就是用回調函數,假如須要一連串的調用,而且後面一個調用依賴前一個返回的結果的時候,就得多層嵌套回調函數,好比下面這種狀況:vue
$('.animateEle').animate({ opacity:'.5' }, 4000,function(){ //回調
$('.animateEle2').animate({ width:'100px' },2000,function(){ //回調
$('.animateEle3').animate({ height:'0' },1000,function(){ .......太亂 }); }); });
回調函數嵌入太多了,看暈了都,代碼很不美觀,因而es6加入了新特性解決這個問題,Promise.jquery
Promise最大的好處就是能夠鏈式的調用函數,起到異步回調函數的做用,看起來更加直觀簡潔,es6
resolve
和 reject
數組
這是Promis最重要的兩個方法,resolve方法可讓Promise對象的狀態變爲成功,reject是失敗,舉個例子一目瞭然:
promise
function print (ready) { return new Promise ((resolve, reject)=>{ if(ready){ resolve("Hello World!"); }else{ reject("Good bye!"); } }) } print(true).then(message=>{ alert(message); },error=>{ alert(error); } );
thenbabel
Promise一般配合then方法來鏈式的使用,then方法裏面第一個回調函數表示成功狀態,也就是resolve,第二個是失敗狀態-reject,若是默認寫一個參數的話,默認resolve異步
代碼會輸出 Hello World!async
一個看不出來,多幾個依賴狀態就看着明顯了:函數
function Print (ready) { return new Promise ((resolve,reject)=>{ if(ready){ resolve("Hello World!"); }else{ reject("Good bye!"); } }); } function print1 () { alert("World"); } function print2 () { alert("!"); } Print(true) .then(message=>{alert(message);}) .then(print1) .then(print2)
上面的代碼依次等到上一個Promise對象返回成功後依次調用,不然按照老式的寫法還得包進回調函數裏,包好幾層就很不方便容易看暈,這樣鏈式的調用取代老式寫法,這是Promise最經常使用的。spa
catch:
當返回發生錯誤的時候,會觸發catch,能夠寫成then(fn).catch(fn)
,至關於 then(fn).then(null, fn);
function Print (ready) { return new Promise ((resolve,reject)=>{ if(ready){ resolve("Hello World!"); }else{ reject("Good bye!"); } }); } function print1 () { alert("World"); } function print2 () { alert("!"); } function catch_error () { alert('error'); } Print(false) .then(message=>{alert(message);}) .then(print1) .then(print2) .catch(catch_error)
失敗的狀態其實能夠寫進then的第二個參數裏,可是通常不用那麼些,用catch捕獲更符合promise的初衷
all:
Promise.all
能夠接收一個元素爲 Promise 對象的數組做爲參數,當這個數組裏面全部的 Promise 對象都變爲 resolve 時,該方法纔會返回
var p1 = new Promise(resolve=>{ setTimeout(()=>{ resolve("Hello"); },3000); }); var p2 = new Promise(resolve=>{ setTimeout(()=>{ resolve("World"); },1000); }); Promise.all([p1, p2]).then(result=>{ console.log(result); });
還有一個和 Promise.all
相相似的方法 Promise.race
,它一樣接收一個數組,不一樣的是隻要該數組中的 Promise 對象的狀態發生變化(不管是 resolve 仍是 reject)該方法都會返回。
es6的Promise還有一些方法,就不寫了,以上是經常使用的.
es7裏還有async函數,也起到異步做用,就很少說了先.
兼容性不是很好,vue開發的時候經常會用babel轉碼,要麼使用jquery的Deferred對象,用法跟Promise差很少。