-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise_chain.js
56 lines (53 loc) · 1.05 KB
/
promise_chain.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Created by lijianxun on 16/9/13.
*/
var Promise = require("bluebird");
function async_func1(name) {
return new Promise((resolve, reject)=>{
setTimeout(()=>{
switch (name){
case "tom":
return resolve({name: name,age: 26});
case "jack":
return resolve({name: name,age: 30});
default:
return reject("无此用户信息");
}
},200);
});
}
function async_func2(userObj) {
return new Promise((resolve, reject)=>{
setTimeout(()=>{
if(userObj.age === 26){
userObj.role = "student";
return resolve(userObj);
}else{
userObj.role = "teacher";
return resolve(userObj);
}
},300);
});
}
//无异常调用
async_func1("tom")
.then((userObj)=>{
return async_func2(userObj);
})
.then((userObj)=>{
console.log("userObj--",userObj);
})
.catch((error)=>{
console.log("error--1 ",error);
});
//有异常调用
async_func1("none")
.then((userObj)=>{
return async_func2(userObj);
})
.then((userObj)=>{
console.log("userObj--",userObj);
})
.catch((error)=>{
console.log("error--1 ",error);
});