Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fecth应该封装个中止请求 #1778

Closed
wushuxuan opened this issue Jun 26, 2018 · 4 comments
Closed

fecth应该封装个中止请求 #1778

wushuxuan opened this issue Jun 26, 2018 · 4 comments

Comments

@wushuxuan
Copy link

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

@sorrycc
Copy link
Member

sorrycc commented Jun 26, 2018

可以提需求给 whatwg/fetch 。

@sorrycc sorrycc closed this as completed Jun 26, 2018
@ybning
Copy link

ybning commented Jul 31, 2018

我也遇到这个问题点 #1838

@neewbee
Copy link

neewbee commented Aug 4, 2018

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;
  })
}

@evanoxu
Copy link

evanoxu commented Mar 14, 2019

可以提需求给 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants