Javascript 是否有某种方法可以发出递归获取请求?

Javascript 是否有某种方法可以发出递归获取请求?,javascript,node.js,express,ecmascript-6,fetch,Javascript,Node.js,Express,Ecmascript 6,Fetch,如果基于响应的HTTP代码(例如:not 200),fetch请求失败,我希望它具有某种重试系统。它看起来像这样: fetch('someURLWithAJSONfile/file.json') .then(function (res) { console.log(res.status); if (res.status !== 200) { console.log("There was an erro

如果基于响应的HTTP代码(例如:not 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请求放在自定义承诺中,并在检查其http响应状态后让它自己调用?

您可以通过将调用包装到一个命名函数中来实现这一点,该函数返回fetch创建的承诺。考虑:

function fetchWithRetry(url, retryLimit, retryCount) {
    retryLimit = retryLimit || Number.MAX_VALUE;
    retryCount = Math.max(retryCount || 0, 0);
    return fetch(url).then(function (res) {
        console.log(res.status);
        if (res.status !== 200 && retryCount < retryLimit) {
            console.log("There was an error processing your fetch request. We are trying again.");
            return fetchWithRetry(url, retryLimit, retryCount + 1);
        } else {
            return res.json();
        }
    });
}

fetchWithRetry('someURLWithAJSONfile/file.json', 10).then(function (json) {
    data = json;
}).catch(function (err) {
    console.log(`There was a problem with the fetch operation: ${err.message}`);
});
函数fetchWithRetry(url、retryLimit、retryCount){
retryLimit=retryLimit | | Number.MAX_值;
retryCount=Math.max(retryCount | | 0,0);
返回fetch(url)。然后返回函数(res){
控制台日志(res.status);
if(res.status!==200&&retryCount

这段代码包装了您现有的调用,并利用闭包作用域来维护重试限制和计数,这两者都是可选的。然后使用URL调用
fetchWithRetry
函数,就像上次调用fetch一样。如果您没有通过重试限制,它将无休止地继续。最后一个
retryCount
变量实际上仅用于递归目的,并用于内部调用。

您可以通过将对fetch的调用包装到一个命名函数中来实现这一点,该函数返回fetch创建的承诺。考虑:

function fetchWithRetry(url, retryLimit, retryCount) {
    retryLimit = retryLimit || Number.MAX_VALUE;
    retryCount = Math.max(retryCount || 0, 0);
    return fetch(url).then(function (res) {
        console.log(res.status);
        if (res.status !== 200 && retryCount < retryLimit) {
            console.log("There was an error processing your fetch request. We are trying again.");
            return fetchWithRetry(url, retryLimit, retryCount + 1);
        } else {
            return res.json();
        }
    });
}

fetchWithRetry('someURLWithAJSONfile/file.json', 10).then(function (json) {
    data = json;
}).catch(function (err) {
    console.log(`There was a problem with the fetch operation: ${err.message}`);
});
函数fetchWithRetry(url、retryLimit、retryCount){
retryLimit=retryLimit | | Number.MAX_值;
retryCount=Math.max(retryCount | | 0,0);
返回fetch(url)。然后返回函数(res){
控制台日志(res.status);
if(res.status!==200&&retryCount

这段代码包装了您现有的调用,并利用闭包作用域来维护重试限制和计数,这两者都是可选的。然后使用URL调用
fetchWithRetry
函数,就像上次调用fetch一样。如果您没有通过重试限制,它将无休止地继续。最后一个
retryCount
变量实际上仅用于递归目的,并用于内部调用。

这里是简单的ES6解决方案(因为您使用的是
fetch
)。
limit
选项表示您希望尝试请求的次数

var doRecursiveRequest = (url, limit = Number.MAX_VALUE) => 
  fetch(url).then(res => {
    if (res.status !== 200 && --limit) {
      return doRecursiveRequest(url, limit);
    } 
    return res.json();
  });

doRecursiveRequest('someURLWithAJSONfile/file.json', 10)
  .then(data => console.log(data))
  .catch(error => console.log(error));

这里是简单的ES6解决方案(因为您使用的是
fetch
)。
limit
选项表示您希望尝试请求的次数

var doRecursiveRequest = (url, limit = Number.MAX_VALUE) => 
  fetch(url).then(res => {
    if (res.status !== 200 && --limit) {
      return doRecursiveRequest(url, limit);
    } 
    return res.json();
  });

doRecursiveRequest('someURLWithAJSONfile/file.json', 10)
  .then(data => console.log(data))
  .catch(error => console.log(error));

当然有。这只是一个承诺,这些承诺可以被链接和嵌套。请向我们展示您尝试的代码确定有。这只是一个承诺,这些承诺可以被链接和嵌套。请向我们展示您尝试的代码此解决方案是否总是在“.then”调用中返回未定义的错误(无论res.status如何)。下面另一个使用传统函数(不是carrot)的解决方案可以正常工作。此解决方案总是在“.then”调用中返回未定义的错误(无论res.status如何)。下面使用传统函数(不是胡萝卜)的另一种解决方案工作正常。