-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch_promise.html
40 lines (38 loc) · 1.13 KB
/
fetch_promise.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Fetch 请求超时</title>
</head>
<body>
<script>
function _fetch(url, timeout = 2000) {
//这里使用Promise.race,以最快 resolve 或 reject 的结果来返回结果
return Promise.race([
fetch(url),
new Promise((resolve, reject) => {
setTimeout(() => {
reject('请求超时');
}, timeout);
})
]);
}
let url0 = 'https://api.github.com/users/tj';
let url1 = 'https://api.github.com/users/etoah';
_fetch(url0, 2000)
.then((res) => {
return res.json();
})
.then((data) => {
console.log('success')
console.log(data)
return data;
})
.catch((err) => {
console.log(err);
});
</script>
</body>
</html>