【发布时间】:2017-09-25 15:35:38
【问题描述】:
如果基于响应的 HTTP 代码(例如:不是 200)以某种方式失败,我希望我的 fetch 请求具有某种重试系统。它看起来像这样:
fetch('someURLWithAJSONfile/file.json')
.then(function (res) {
console.log(res.status);
if (res.status !== 200) {
console.log("There was an error processing your fetch request. We are trying again.");
// Recursive call to same fetch request until succeeds
} else {
return res.json();
}
}).then(function (json) {
data = json;
}).catch(function (err) {
console.log(`There was a problem with the fetch operation: ${err.message}`);
});
有没有办法将 fetch 请求放入自定义 Promise 并在检查其 http 响应状态后使其调用自己?
【问题讨论】:
-
当然有。这只是一个承诺,并且可以链接和嵌套。请向我们展示您的尝试代码。
-
data = json;是 what won't work
标签: javascript node.js express ecmascript-6 fetch