fetch的问题

fetch的问题

七月 11, 2017

使用 fetch 的时候遇到了一个比较奇怪的问题,

我的代码是这样的:

1
2
3
4
5
6
7
fetch(api)
.then(this.json)
.then(this.unWrap)
.catch((error) => {
console.warn("get-error: " + error + ", api=" + api);
return {success: false, msg: error}
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
static json = (response) => {
console.log("response", response);
let json = response.json();
console.log("json", json);
setTimeout(() => null, 0); //加了这句后可解决问题
//问题是这样的,正常来说,执行完 json 就会立即执行 unWrap,
//但是程序在 log 了 json 后就没有往下走了,
return json;
};
static unWrap = (responseJson) => {
console.log("responseJson", responseJson);
if (responseJson.status !== 0 && responseJson.status !== 200) {
throw responseJson.msg;
}
return {success: true, data: responseJson.data};
};

更多信息可到:issues-6679