JS - Promise使用详解2(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 的回调|
1
2
3
4
5
|
cook()
.then(eat)
.
catch
(
function
(data){
console.log(data +
'没法吃!'
);
});
|
(2)它的另一个作用是,当执行 resolve 的回调(也就是上面 then 中的第一个参数)时,如果抛出异常了(代码出错了),那么也不会报错卡死 js,而是会进到这个 catch 方法中。
|
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);
});
|
- 如果 5 秒内图片请求成功那么便进入 then 方法,执行正常的流程。
- 如果 5 秒钟图片还未成功返回,那么则进入 catch,报“图片请求超时”的信息。
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_1638.html






859



被折叠的 条评论
为什么被折叠?



