-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetchPro.js
81 lines (76 loc) · 1.49 KB
/
fetchPro.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//fetch 基础用法
fetch(url, {
method: 'POST',
headers: {
Accept: '*/*',
'Content-Type': 'application/json;charset=utf-8',
'access-token': 'token',
},
})
.then(res => {
res.json();
})
.then(data => {
console.log(data);
})
.catch(err => {
console.log(err.msg);
});
/*
封装之后
*/
/**
* @param {string} url 请求路径
* @param {string} method 请求方式
* @param {object} headers 请求头
* @param {object} body 请求体
* @return {promise} result 请求结果
*/
const fetchPro = (url, method, header, body) => {
const defaultHeader = {
'Content-Type': 'application/json',
};
let params;
if (method.toUpperCase() === 'GET') {
params = undefined;
} else {
params = {
headers: {
...defaultHeader,
header,
},
method,
body: JSON.stringify(body),
};
}
const p = new Promise((resolve, reject) => {
fetch(url, params)
.then(res => res.json())
.then(res => {
if (res.status === 200) {
resolve(res);
} else {
//做一些错误提示
//...
reject(res.errMsg);
}
})
.catch(err => {
//提示
reject(err);
})
.finally(() => {
//处理一些东西,比如setLoading(false)
});
});
//如果有超时处理啥的就一起放进去
return Promise.race([p]);
};
/*
更多:
关于 react
自定义一个 useHttpHook
传入 watch 监听项 配合 useEffect
useState 控制 isLoading
捕获错误进行提示
*/