Promise超时情况

Posted rusr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Promise超时情况相关的知识,希望对你有一定的参考价值。

export const ERROR_PROMISE_TIMEOUT = ‘ERROR_PROMISE_TIMEOUT‘;

export default function (promise, timeout) {
  let timer = undefined;
  return Promise.race([
    new Promise(function (resolve, reject) {
      timer = setTimeout(() => {
        reject(new Error(ERROR_PROMISE_TIMEOUT));
      }, timeout)
    }),
    promise.then(res => {
      timer && window.clearTimeout(timer);
      return res;
    }).catch(err => {
      timer && window.clearTimeout(timer);
      return Promise.reject(err);
    })
  ])
}

 

以上是关于Promise超时情况的主要内容,如果未能解决你的问题,请参考以下文章