第一部分:html
JS - Promise使用詳解1(基本概念、使用優勢)
1、promises相關概念
1,then()方法介紹
- 成功回調
- 失敗回調
- 前進回調(規範沒有要求包括前進回調的實現,可是不少都實現了)。
2,Promise對象狀態
- Pending(進行中、未完成的)
- Resolved(已完成,又稱 Fulfilled)
- Rejected(已失敗)。
3,Promise/A規範圖解
4,目前支持Promises/A規範的庫
- Q:能夠在NodeJS 以及瀏覽器上工做,與jQuery兼容,能夠經過消息傳遞遠程對象。
- RSVP.js:一個輕量級的庫,它提供了組織異步代碼的工具。
- when.js:體積小巧,使用方便。
- NodeJS的Promise
- jQuery 1.5:聽說是基於「CommonJS Promises/A」規範
- WinJS / Windows 8 / Metro
2、使用promises的優點
1,解決回調地獄(Callback Hell)問題
1
2
3
4
5
6
7
8
9
10
11
12
|
firstAsync(
function
(data){
//處理獲得的 data 數據
//....
secondAsync(
function
(data2){
//處理獲得的 data2 數據
//....
thirdAsync(
function
(data3){
//處理獲得的 data3 數據
//....
});
});
});
|
(2)若是使用 promises 的話,代碼就會變得扁平且更可讀了。前面提到 then 返回了一個 promise,所以咱們能夠將 then 的調用不停地串連起來。其中 then 返回的 promise 裝載了由調用返回的值。ajax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
firstAsync()
.then(
function
(data){
//處理獲得的 data 數據
//....
return
secondAsync();
})
.then(
function
(data2){
//處理獲得的 data2 數據
//....
return
thirdAsync();
})
.then(
function
(data3){
//處理獲得的 data3 數據
//....
});
|
2,更好地進行錯誤捕獲
(1)好比下面代碼咱們使用 setTimeout 模擬異步操做,在其中拋出了個異常。但因爲異步回調中,回調函數的執行棧與原函數分離開,致使外部沒法抓住異常。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
function
fetch(callback) {
setTimeout(() => {
throw
Error(
'請求失敗'
)
}, 2000)
}
try
{
fetch(() => {
console.log(
'請求處理'
)
// 永遠不會執行
})
}
catch
(error) {
console.log(
'觸發異常'
, error)
// 永遠不會執行
}
// 程序崩潰
// Uncaught Error: 請求失敗
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
function
fetch(callback) {
return
new
Promise((resolve, reject) => {
setTimeout(() => {
reject(
'請求失敗'
);
}, 2000)
})
}
fetch()
.then(
function
(data){
console.log(
'請求處理'
);
console.log(data);
},
function
(reason, data){
console.log(
'觸發異常'
);
console.log(reason);
}
);
|
固然咱們在 catch 方法中處理 reject 回調也是能夠的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
function
fetch(callback) {
return
new
Promise((resolve, reject) => {
setTimeout(() => {
reject(
'請求失敗'
);
}, 2000)
})
}
fetch()
.then(
function
(data){
console.log(
'請求處理'
);
console.log(data);
}
)
.
catch
(
function
(reason){
console.log(
'觸發異常'
);
console.log(reason);
});
|
第二部分:數組
JS - Promise使用詳解2(ES6中的Promise)
2015年6月, ES2015(即 ECMAScript 六、ES6) 正式發佈。其中 Promise 被列爲正式規範,成爲 ES6 中最重要的特性之一。promise
1,then()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
//作飯
function
cook(){
console.log(
'開始作飯。'
);
var
p =
new
Promise(
function
(resolve, reject){
//作一些異步操做
setTimeout(
function
(){
console.log(
'作飯完畢!'
);
resolve(
'雞蛋炒飯'
);
}, 1000);
});
return
p;
}
//吃飯
function
eat(data){
console.log(
'開始吃飯:'
+ data);
var
p =
new
Promise(
function
(resolve, reject){
//作一些異步操做
setTimeout(
function
(){
console.log(
'吃飯完畢!'
);
resolve(
'一塊碗和一雙筷子'
);
}, 2000);
});
return
p;
}
function
wash(data){
console.log(
'開始洗碗:'
+ data);
var
p =
new
Promise(
function
(resolve, reject){
//作一些異步操做
setTimeout(
function
(){
console.log(
'洗碗完畢!'
);
resolve(
'乾淨的碗筷'
);
}, 2000);
});
return
p;
}
|
(2)使用 then 鏈式調用這三個方法:瀏覽器
1
2
3
4
5
6
7
8
9
10
|
cook()
.then(
function
(data){
return
eat(data);
})
.then(
function
(data){
return
wash(data);
})
.then(
function
(data){
console.log(data);
});
|
固然上面代碼還能夠簡化成以下:異步
1
2
3
4
5
6
|
cook()
.then(eat)
.then(wash)
.then(
function
(data){
console.log(data);
});
|
(3)運行結果以下:函數
2,reject()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
//作飯
function
cook(){
console.log(
'開始作飯。'
);
var
p =
new
Promise(
function
(resolve, reject){
//作一些異步操做
setTimeout(
function
(){
console.log(
'作飯失敗!'
);
reject(
'燒焦的米飯'
);
}, 1000);
});
return
p;
}
//吃飯
function
eat(data){
console.log(
'開始吃飯:'
+ data);
var
p =
new
Promise(
function
(resolve, reject){
//作一些異步操做
setTimeout(
function
(){
console.log(
'吃飯完畢!'
);
resolve(
'一塊碗和一雙筷子'
);
}, 2000);
});
return
p;
}
cook()
.then(eat,
function
(data){
console.log(data +
'無法吃!'
);
})
|
運行結果以下:工具
1
2
3
4
|
cook()
.then(
null
,
function
(data){
console.log(data +
'無法吃!'
);
})
|
3,catch()方法
(1)它能夠和 then 的第二個參數同樣,用來指定 reject 的回調post
1
2
3
4
5
|
cook()
.then(eat)
.
catch
(
function
(data){
console.log(data +
'無法吃!'
);
});
|
(2)它的另外一個做用是,當執行 resolve 的回調(也就是上面 then 中的第一個參數)時,若是拋出異常了(代碼出錯了),那麼也不會報錯卡死 js,而是會進到這個 catch 方法中。fetch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
//作飯
function
cook(){
console.log(
'開始作飯。'
);
var
p =
new
Promise(
function
(resolve, reject){
//作一些異步操做
setTimeout(
function
(){
console.log(
'作飯完畢!'
);
resolve(
'雞蛋炒飯'
);
}, 1000);
});
return
p;
}
//吃飯
function
eat(data){
console.log(
'開始吃飯:'
+ data);
var
p =
new
Promise(
function
(resolve, reject){
//作一些異步操做
setTimeout(
function
(){
console.log(
'吃飯完畢!'
);
resolve(
'一塊碗和一雙筷子'
);
}, 2000);
});
return
p;
}
cook()
.then(
function
(data){
throw
new
Error(
'米飯被打翻了!'
);
eat(data);
})
.
catch
(
function
(data){
console.log(data);
});
|
運行結果以下:
1
2
3
4
5
6
7
8
9
10
11
|
somePromise.then(
function
() {
return
a();
}).
catch
(TypeError,
function
(e) {
//If a is defined, will end up here because
//it is a type error to reference property of undefined
}).
catch
(ReferenceError,
function
(e) {
//Will end up here if a wasn't defined at all
}).
catch
(
function
(e) {
//Generic catch-the rest, error wasn't TypeError nor
//ReferenceError
});
|
4,all()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
//切菜
function
cutUp(){
console.log(
'開始切菜。'
);
var
p =
new
Promise(
function
(resolve, reject){
//作一些異步操做
setTimeout(
function
(){
console.log(
'切菜完畢!'
);
resolve(
'切好的菜'
);
}, 1000);
});
return
p;
}
//燒水
function
boil(){
console.log(
'開始燒水。'
);
var
p =
new
Promise(
function
(resolve, reject){
//作一些異步操做
setTimeout(
function
(){
console.log(
'燒水完畢!'
);
resolve(
'燒好的水'
);
}, 1000);
});
return
p;
}
Promise
.all([cutUp(), boil()])
.then(
function
(results){
console.log(
"準備工做完畢:"
);
console.log(results);
});
|
(2)運行結果以下:
5,race()方法
1
2
3
4
5
6
|
Promise
.race([cutUp(), boil()])
.then(
function
(results){
console.log(
"準備工做完畢:"
);
console.log(results);
});
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
//請求某個圖片資源
function
requestImg(){
var
p =
new
Promise(
function
(resolve, reject){
var
img =
new
Image();
img.onload =
function
(){
resolve(img);
}
img.src =
'xxxxxx'
;
});
return
p;
}
//延時函數,用於給請求計時
function
timeout(){
var
p =
new
Promise(
function
(resolve, reject){
setTimeout(
function
(){
reject(
'圖片請求超時'
);
}, 5000);
});
return
p;
}
Promise
.race([requestImg(), timeout()])
.then(
function
(results){
console.log(results);
})
.
catch
(
function
(reason){
console.log(reason);
});
|
上面代碼 requestImg 函數異步請求一張圖片,timeout 函數是一個延時 5 秒的異步操做。咱們將它們一塊兒放在 race 中賽跑。
- 若是 5 秒內圖片請求成功那麼便進入 then 方法,執行正常的流程。
- 若是 5 秒鐘圖片還未成功返回,那麼則進入 catch,報「圖片請求超時」的信息。
第三部分:
JS - Promise使用詳解3(jQuery中的Deferred)
上文我介紹了 ES6 中的 Promise,它徹底遵循 Promises/A 規範。而咱們熟悉的 jQuery 又有本身的 Promise 實現:Deferred(但其並非遵循 Promises/A 規範)。本文就講講 jQuery 中 Promise 的實現。
1、Deferred對象及其方法
1,$.Deferred
- jQuery 用 $.Deferred 實現了 Promise 規範。
- $.Deferred() 返回一個對象,咱們能夠稱之爲 Deferred 對象,上面掛着一些熟悉的方法如:done、fail、then 等。
- jQuery 就是用這個 Deferred 對象來註冊異步操做的回調函數,修改並傳遞異步操做的狀態。
下面咱們定義作飯、吃飯、洗碗(cook、eat、wash)這三個方法(這裏使用 setTimeout 模擬異步操做)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
//作飯
function
cook(){
console.log(
'開始作飯。'
);
var
def = $.Deferred();
//執行異步操做
setTimeout(
function
(){
console.log(
'作飯完畢!'
);
def.resolve(
'雞蛋炒飯'
);
}, 1000);
return
def.promise();
}
//吃飯
function
eat(data){
console.log(
'開始吃飯:'
+ data);
var
def = $.Deferred();
//執行異步操做
setTimeout(
function
(){
console.log(
'吃飯完畢!'
);
def.resolve(
'一塊碗和一雙筷子'
);
}, 1000);
return
def.promise();
}
//洗碗
function
wash(data){
console.log(
'開始洗碗:'
+ data);
var
def = $.Deferred();
//執行異步操做
setTimeout(
function
(){
console.log(
'洗碗完畢!'
);
def.resolve(
'乾淨的碗筷'
);
}, 1000);
return
def.promise();
}
|
2,then()方法
1
2
3
4
5
6
7
8
9
10
|
cook()
.then(
function
(data){
return
eat(data);
})
.then(
function
(data){
return
wash(data);
})
.then(
function
(data){
console.log(data);
});
|
固然也能夠簡寫成以下:
1
2
3
4
5
6
|
cook()
.then(eat)
.then(wash)
.then(
function
(data){
console.log(data);
});
|
(2)運行結果以下:
3,reject()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
//作飯
function
cook(){
console.log(
'開始作飯。'
);
var
def = $.Deferred();
//執行異步操做
setTimeout(
function
(){
console.log(
'作飯完畢!'
);
def.reject(
'燒焦的米飯'
);
}, 1000);
return
def.promise();
}
//吃飯
function
eat(data){
console.log(
'開始吃飯:'
+ data);
var
def = $.Deferred();
//執行異步操做
setTimeout(
function
(){
console.log(
'吃飯完畢!'
);
def.resolve(
'一塊碗和一雙筷子'
);
}, 1000);
return
def.promise();
}
cook()
.then(eat,
function
(data){
console.log(data +
'無法吃!'
);
})
|
運行結果以下:
1
|
deferred.then( doneFilter [, failFilter ] [, progressFilter ] )
|
4,done()與fail()方法
done 和 fail 是 jQuery 增長的兩個語法糖方法。分別用來指定執行完成和執行失敗的回調。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//then方法
d.then(
function
(){
console.log(
'執行完成'
);
},
function
(){
console.log(
'執行失敗'
);
});
//done方法、fail方法
d.done(
function
(){
console.log(
'執行完成'
);
})
.fail(
function
(){
console.log(
'執行失敗'
);
});
|
5,always()方法
jQuery 的 Deferred 對象上還有一個 always 方法,不論執行完成仍是執行失敗,always 都會執行,有點相似 ajax 中的 complete。
1
2
3
4
5
6
|
cook()
.then(eat)
.then(wash)
.always(
function
(){
console.log(
'上班去!'
);
})
|
2、與Promises/A規範的差別
在開頭講到,目前 Promise 事實上的標準是社區提出的 Promises/A 規範,jQuery 的實現並不徹底符合 Promises/A,主要表如今對錯誤的處理不一樣。
1,ES6中對錯誤的處理
1
2
3
4
5
6
7
8
|
cook()
.then(
function
(data){
throw
new
Error(
'米飯被打翻了!'
);
eat(data);
})
.
catch
(
function
(data){
console.log(data);
});
|
2,jQuery中對錯誤的處理
一樣咱們在回調函數中拋出一個錯誤,jQuery 的 Deferred 對象此時不會改變狀態,亦不會觸發回調函數,該錯誤通常狀況下會被 window.onerror 捕獲。換句話說,在 Deferred 對象中,老是必須使用 reject 方法來改變狀態。
1
2
3
4
5
6
7
8
9
10
|
cook()
.then(
function
(data){
throw
new
Error(
'米飯被打翻了!'
);
eat(data);
})
window.onerror =
function
(msg, url, line) {
console.log(
"發生錯誤了:"
+ msg);
return
true
;
//若是註釋掉該語句,瀏覽器中仍是會有錯誤提示,反之則沒有。
}
|
3、$.when方法
- $.when 並無定義在 $.Deferred 中,看名字就知道,$.when 它是一個單獨的方法。
- $.when 與 ES6 的 all 的參數稍有區別,它接受的並非數組,而是多個 Deferred 對象。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
//切菜
function
cutUp(){
console.log(
'開始切菜。'
);
var
def = $.Deferred();
//執行異步操做
setTimeout(
function
(){
console.log(
'切菜完畢!'
);
def.resolve(
'切好的菜'
);
}, 1000);
return
def.promise();
}
//燒水
function
boil(){
console.log(
'開始燒水。'
);
var
def = $.Deferred();
//執行異步操做
setTimeout(
function
(){
console.log(
'燒水完畢!'
);
def.resolve(
'燒好的水'
);
}, 1000);
return
def.promise();
}
$.when(cutUp(), boil())
.then(
function
(data1, data2){
console.log(
"準備工做完畢:"
);
console.log(data1, data2);
});
|
4、Ajax函數與Deferred的關係
jQuery 中咱們經常會用到的 ajax, get, post 等 Ajax 函數,其實它們內部都已經實現了 Deferred。這些方法調用後會返回一個受限的 Deferred 對象。既然是 Deferred 對象,那麼天然也有上面提到的全部特性。
1,then方法
1
2
3
4
5
6
7
8
9
10
11
12
13
|
req1 =
function
(){
return
$.ajax(
/*...*/
);
}
req2 =
function
(){
return
$.ajax(
/*...*/
);
}
req3 =
function
(){
return
$.ajax(
/*...*/
);
}
req1().then(req2).then(req3).done(
function
(){
console.log(
'請求發送完畢'
);
});
|
2,success、error與complete方法
success、error、complete是 ajax 提供的語法糖,功能與 Deferred 對象的 done、fail、always 一致。好比下面兩段代碼功能是一致的:
1
2
3
4
5
6
7
8
9
10
11
|
//使用success、error、complete
$.ajax(
/*...*/
)
.success(
function
(){
/*...*/
})
.error(
function
(){
/*...*/
})
.complete(
function
(){
/*...*/
})
//使用done、fail、always
$.ajax(
/*...*/
)
.done(
function
(){
/*...*/
})
.fai(
function
(){
/*...*/
})
.always(
function
(){
/*...*/
})
|
原文出自:www.hangge.com 轉載請保留原文連接:http://www.hangge.com/blog/cache/detail_1639.html
原文出自:www.hangge.com 轉載請保留原文連接:http://www.hangge.com/blog/cache/detail_1638.html
原文出自:www.hangge.com 轉載請保留原文連接:http://www.hangge.com/blog/cache/detail_1635.html