1
2
3
4
5
6
7
8
9
10
11
12
|
firstAsync(
function
(data){
//处理获得的 data 数据
//....
secondAsync(
function
(data2){
//处理获得的 data2 数据
//....
thirdAsync(
function
(data3){
//处理获得的 data3 数据
//....
});
});
});
|
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 数据
//....
});
|
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);
}
);
|
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);
});
|
原文出自:www.hangge.com 转载请保留原文连接:https://www.hangge.com/blog/cache/detail_1635.htmlhtml