第一部分: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