Dva: fecth应该封装个中止请求

Created on 26 Jun 2018  ·  4Comments  ·  Source: dvajs/dva

页面离开之后,要关闭一些正在等待中的请求,所以要中断这些无用的请求

Most helpful comment

可以提需求给 whatwg/fetch 。

关于如何取消 fetch,曾有过许多讨论,也有很多的方案被提出来。

1 最初的讨论:whatwg/fetch#27
2 Cancelable promise 的方案被否了:tc39/proposal-cancelable-promises#4
3 其它方案继续讨论:whatwg/fetch#447
4 讨论出的提案:whatwg/fetch#447 (comment)
5 whatwg 最终给出的标准:https://dom.spec.whatwg.org/#dom-abortcontroller-abortcontroller
6 MDN 使用文档:https://developer.mozilla.org/en-US/docs/Web/API/AbortController/AbortController

All 4 comments

可以提需求给 whatwg/fetch 。

我也遇到这个问题点 https://github.com/dvajs/dva/issues/1838

https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort

var controller = new AbortController();
var signal = controller.signal;

var downloadBtn = document.querySelector('.download');
var abortBtn = document.querySelector('.abort');

downloadBtn.addEventListener('click', fetchVideo);

abortBtn.addEventListener('click', function() {
  controller.abort();
  console.log('Download aborted');
});

function fetchVideo() {
  ...
  fetch(url, {signal}).then(function(response) {
    ...
  }).catch(function(e) {
    reports.textContent = 'Download error: ' + e.message;
  })
}

可以提需求给 whatwg/fetch 。

关于如何取消 fetch,曾有过许多讨论,也有很多的方案被提出来。

1 最初的讨论:whatwg/fetch#27
2 Cancelable promise 的方案被否了:tc39/proposal-cancelable-promises#4
3 其它方案继续讨论:whatwg/fetch#447
4 讨论出的提案:whatwg/fetch#447 (comment)
5 whatwg 最终给出的标准:https://dom.spec.whatwg.org/#dom-abortcontroller-abortcontroller
6 MDN 使用文档:https://developer.mozilla.org/en-US/docs/Web/API/AbortController/AbortController

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zouyan532 picture zouyan532  ·  3Comments

oldfeel picture oldfeel  ·  3Comments

huyawei picture huyawei  ·  3Comments

wm3445 picture wm3445  ·  3Comments

sorrycc picture sorrycc  ·  3Comments