递归获取/承诺以防止在可能较长的后端处理期间超时

时间:2018-06-29 23:50:27

标签: javascript recursion syntax promise timeout

当我的网络服务器验证新的WiFi设置时,如果凭据良好,则验证会迅速结束,但如果凭据不正确,则需要一段时间才能超时。我想在验证成功时让用户保持尽可能短的时间,同时防止浏览器在花费更长的时间后超时。

我找到了一个递归提取/承诺调用的例子,它看起来应该可以工作,但是不行。

有人看到我想念的东西吗?

<html><head>
<script>const fetchWithRetry = (ThisURL) => {
let abort = false;
const options = {
  url: ThisURL,
  options: {},
  cancel: {},
  retries: 30,
  retryDelay: 1000
};

// Add an abort to the cancel object.
cancel.abort = () => { abort = true };

// Abort or proceed?
return abort ? Promise.reject('aborted') : fetch(options.url).then(response => {

  // Reject because of abort
 return abort ? Promise.reject('aborted')

  // Response is good 
  : response.ok ? Promise.resolve(response.text()) 

  // Retries exceeded
  : !options.retries ? Promise.reject('retries exceeded')

  // Retry with one less retry
  : new Promise((resolve, reject){
      setTimeout(() => {

        // We use the returned promise's resolve and reject as
        // callback so that the nested call propagates backwards.
        fetchWithRetry({ ThisURL, retries: retries - 1 }).then(resolve, reject);
      }, options.retryDelay);

    });
  });
}</script>
<title>'Verifying WiFi Settings'</title>
</head>
<body onload="fetchWithRetry('/WiFiValid')">
<h1>Please wait while WiFi settings are verified</h1>
</body>
</html>

0 个答案:

没有答案