如何取消已中止的获取

huangapple go评论91阅读模式
英文:

How unabort the aborted fetch

问题

  1. const startFetch = document.querySelector("#startFetch");
  2. const stopFetch = document.querySelector("#stopFetch");
  3. const controller = new AbortController();
  4. const todos = async() => {
  5. fetch("https://jsonplaceholder.typicode.com/todos", {
  6. signal: controller.signal,
  7. })
  8. .then((data) => data.json())
  9. .then((todos) => {
  10. console.log(todos);
  11. })
  12. .catch((err) => {
  13. console.log(err.message);
  14. });
  15. };
  16. startFetch.addEventListener("click", todos);
  17. stopFetch.addEventListener("click", () => {
  18. controller.abort();
  19. });
  1. <button id="startFetch">startFetch</button>
  2. <button id="stopFetch">stopFetch</button>

I need to un-abort an aborted fetch

  1. <details>
  2. <summary>英文:</summary>
  3. I aborted, fetched but I couldn&#39;t call the fetch again

const startFetch = document.querySelector(" #startFetch");

  1. &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
  2. &lt;!-- language: lang-js --&gt;
  3. const stopFetch = document.querySelector(&quot;#stopFetch&quot;);
  4. const controller = new AbortController();
  5. const todos = async() =&gt; {
  6. fetch(&quot;https://jsonplaceholder.typicode.com/todos&quot;, {
  7. signal: controller.signal,
  8. })
  9. .then((data) =&gt; data.json())
  10. .then((todos) =&gt; {
  11. console.log(todos);
  12. })
  13. .catch((err) =&gt; {
  14. console.log(err.message);
  15. });
  16. };
  17. startFetch.addEventListener(&quot;click&quot;, todos);
  18. stopFetch.addEventListener(&quot;click&quot;, () =&gt; {
  19. controller.abort();
  20. });
  21. &lt;!-- language: lang-html --&gt;
  22. &lt;button id=&quot;startFetch&quot;&gt;startFetch&lt;/button&gt;
  23. &lt;button id=&quot;stopFetch&quot;&gt;stopFetch&lt;/button&gt;
  24. &lt;!-- end snippet --&gt;
  25. I need to un-abort an aborted fetch
  26. </details>
  27. # 答案1
  28. **得分**: 1
  29. 无法。一个被中止的获取操作关闭了连接,你需要发起一个单独的新请求。一个被中止的信号保持了其状态,你需要创建一个新的信号(以及一个新的 `AbortController`)。
  30. ```javascript
  31. const stopFetch = document.querySelector("#stopFetch");
  32. let controller;
  33. const todos = async() => {
  34. controller = new AbortController();
  35. fetch("https://jsonplaceholder.typicode.com/todos", {
  36. signal: controller.signal,
  37. })
  38. .then(data => data.json())
  39. .then(todos => {
  40. console.log(todos);
  41. }, err => {
  42. console.log(err.message);
  43. }).finally(() => {
  44. controller = null;
  45. });
  46. };
  47. startFetch.addEventListener("click", todos);
  48. stopFetch.addEventListener("click", () => {
  49. if (controller) {
  50. controller.abort();
  51. } else {
  52. console.log("no active request");
  53. }
  54. });
  1. <button id="startFetch">startFetch</button>
  2. <button id="stopFetch">stopFetch</button>
英文:

You cannot. An aborted fetch closed the connection, you need to make a separate new request. An aborted signal stays keeps that state, you need to create a new signal (and a new AbortController).

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

  1. const stopFetch = document.querySelector(&quot;#stopFetch&quot;);
  2. let controller;
  3. const todos = async() =&gt; {
  4. controller = new AbortController();
  5. fetch(&quot;https://jsonplaceholder.typicode.com/todos&quot;, {
  6. signal: controller.signal,
  7. })
  8. .then(data =&gt; data.json())
  9. .then(todos =&gt; {
  10. console.log(todos);
  11. }, err =&gt; {
  12. console.log(err.message);
  13. }).finally(() =&gt; {
  14. controller = null;
  15. });
  16. };
  17. startFetch.addEventListener(&quot;click&quot;, todos);
  18. stopFetch.addEventListener(&quot;click&quot;, () =&gt; {
  19. if (controller) {
  20. controller.abort();
  21. } else {
  22. console.log(&quot;no active request&quot;);
  23. }
  24. });

<!-- language: lang-html -->

  1. &lt;button id=&quot;startFetch&quot;&gt;startFetch&lt;/button&gt;
  2. &lt;button id=&quot;stopFetch&quot;&gt;stopFetch&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月26日 04:42:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定