{"id":10694,"date":"2023-01-17T23:07:36","date_gmt":"2023-01-17T15:07:36","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10694"},"modified":"2023-02-03T00:11:44","modified_gmt":"2023-02-02T16:11:44","slug":"fetch-abortcontroller-abort-fetch-axios","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2023\/01\/fetch-abortcontroller-abort-fetch-axios\/","title":{"rendered":"\u4f7f\u7528AbortController abort\u4e2d\u65ad\u539f\u751ffetch\u6216axios\u8bf7\u6c42"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10694\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10694<\/a> \u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202301-fetch-abort.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202301\/abort-cover.jpg\" width=\"480\" height=\"304\" alt=\"\u5360\u4f4d\u56fe\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001AbortController\u7684\u4f5c\u7528<\/h3>\n<p>AbortController\u53ef\u4ee5\u7528\u6765\u7ec8\u6b62\u4e00\u4e2a\u6216\u591a\u4e2aWeb\u8bf7\u6c42\u3002<\/p>\n<p>\u6211\u5927\u81f4\u770b\u4e86\u4e0b\uff0c\u6b64API\u53ef\u4ee5\u7ec8\u6b62fetch\u8bf7\u6c42\uff0c\u4efb\u610f\u54cd\u5e94\u4e3b\u4f53\u548c\u6d41\u3002<\/p>\n<p>\u65e5\u5e38\u5f00\u53d1\uff0c\u7528\u7684\u6700\u591a\u7684\u5c31\u662f\u5bf9 fetch \u8bf7\u6c42\u7684\u7ec8\u6b62\uff0c\u7531\u4e8eaxios\uff08\u77e5\u540dHTTP \u5e93\uff0c\u57fa\u4e8epromise\uff0c\u53ef\u4ee5\u7528\u5728\u6d4f\u89c8\u5668\u548cnode.js \u4e2d\uff09\u7684\u5e95\u5c42\u662f\u4f7f\u7528fetch\u5b9e\u73b0\u7684\uff0c\u56e0\u6b64\uff0cAbortController\u4e5f\u80fd\u7528\u6765\u7ec8\u6b62axios\u53d1\u9001\u7684\u8bf7\u6c42\u3002<\/p>\n<p>\u4f7f\u7528\u7684\u65f6\u5019\u5148\u6784\u9020\u3002<\/p>\n<pre>controller = new AbortController();<\/pre>\n<p>\u652f\u6301\u4e00\u4e2a\u5c5e\u6027\u548c\u4e00\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<dl>\n<dt><strong>AbortController.signal<\/strong><\/dt>\n<dd>\u8fd4\u56deAbortSignal\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u9700\u8981\u8bbe\u7f6e\u5728fetch\u8bf7\u6c42\u4e2d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u5916\u90e8\u7ec8\u6b62\u8fd9\u4e2a\u8bf7\u6c42\u4e86\u3002\u76f8\u5f53\u4e8e\u5728fetch\u8bf7\u6c42\u5185\u90e8\u5d4c\u5165\u4e00\u4e2a\u6293\u624b\uff0c\u6216\u8005\u8bf4\u9884\u57cb\u4e00\u4e2a\u5f00\u5173\u3002<\/p>\n<p>\u5177\u4f53\u5982\u4f55\u4f7f\u7528\u53c2\u89c1\u4e0b\u4e00\u8282\u7684\u6848\u4f8b\u548c\u4ee3\u7801\u793a\u610f\u3002<\/dd>\n<dt><strong>AbortController.abort()<\/strong><\/dt>\n<dd>\u7ec8\u6b62\u8bf7\u6c42\u7684\u65b9\u6cd5\u3002\u53ea\u8981\u8bf7\u6c42\u8fd8\u6ca1\u6709\u5b8c\u6210\uff0c\u90fd\u662f\u53ef\u4ee5\u7ec8\u6b62\u7684\u3002<\/dd>\n<\/dl>\n<h3>\u4e8c\u3001\u4e2d\u65ad\u8bf7\u6c42\u793a\u610f<\/h3>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u5177\u4f53\u5982\u4f55\u4f7f\u7528\uff0c\u8fd8\u9700\u8981demo\u51fa\u9a6c\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202301\/js-fetch-abort-demo.php\" rel=\"noopener\" target=\"_blank\">AbortController\u53d6\u6d88fetch\u8bf7\u6c42demo<\/a><\/p>\n<p>\u4e0b\u56feGIF\u662f\u6f14\u793a\u793a\u610f\uff0c\u6211\u4eec\u5feb\u901f\u70b9\u51fb\u9875\u9762\u4e2d\u7684\u51e0\u4e2a\u6309\u94ae\uff0c\u4e00\u5b9a\u662f\u663e\u793a\u6700\u540e\u4e00\u4e2a\u6309\u94ae\u70b9\u51fb\u5bf9\u5e94\u7684\u8fd4\u56de\u6570\u636e\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4f9d\u6b21\u70b9\u51fb&#8221;zhang&#8221;, &#8220;xin&#8221;, &#8220;xu&#8221; \u4e09\u4e2a\u6309\u94ae\uff0c\u8fd4\u56de\u503c\u662f &#8216;xu&#8217;\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202301\/fetch-abort.gif\" width=\"359\" height=\"206\" alt=\"\u70b9\u51fb\u6309\u94ae\u4e0e\u8fd4\u56de\u503c\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6b64\u65f6\u6253\u5f00\u6d4f\u89c8\u5668\u63a7\u5236\u53f0\uff0c\u67e5\u770bnetwork\u7f51\u7edc\u8bf7\u6c42\uff0c\u53ef\u4ee5\u770b\u5230\u524d\u9762\u4e24\u4e2a\u6309\u94ae\u7684\u8bf7\u6c42\u88ab\u7ec8\u6b62\u53d6\u6d88\u4e86\uff0c\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202301\/2023-01-17_223118.png\" width=\"627\" height=\"151\" alt=\"\u8bf7\u6c42\u88ab\u53d6\u6d88\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u903b\u8f91\u5b9e\u73b0\u7684\u6838\u5fc3\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff0c\u5373\uff0c\u5982\u679c\u8bf7\u6c42\u6267\u884c\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u4e0a\u4e00\u4e2a\u8bf7\u6c42\u8fd8\u6ca1\u5b8c\u6210\uff0c\u5c31abort\u963b\u6b62\uff0c\u540c\u65f6\uff0c\u521b\u5efa\u65b0\u7684<code>AbortController<\/code>\u4e2d\u65ad\u5bf9\u8c61\u3002<\/p>\n<pre>const data = {};\r\n\/\/ \u6784\u9020\u5668\u5bf9\u8c61\r\nlet controller = null;\r\n\r\n\/\/ \u6309\u94ae\u70b9\u51fb\r\nbutton.addEventListener('click', function (event) {\r\n  \/\/ \u6784\u9020 AbortController \u5bf9\u8c61\r\n  if (data.controller) {\r\n    controller = data.controller;\r\n  } else {\r\n    controller = new AbortController();\r\n    data.controller = controller;\r\n  }\r\n  \/\/ \u5982\u679c\u6b63\u5728\u8bf7\u6c42\uff0c\u7ec8\u6b62\r\n  if (data.loading) {\r\n    controller.abort();\r\n\r\n    \/\/ \u6784\u5efa\u65b0\u7684 AbortController\r\n    controller = new AbortController();\r\n    data.controller = controller;\r\n  }\r\n\r\n  data.loading = true;\r\n\r\n  \/\/ \u8bf7\u6c42\u903b\u8f91\r\n  fetch('.\/getVal.php?val=' + event.target.value, {\r\n    signal: controller.signal\r\n  }).then(res =&amp;gt; res.text()).then(text =&amp;gt; {\r\n    \/\/ text \u5c31\u662f\u8fd4\u56de\u5185\u5bb9\r\n  });\r\n});<\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u4e0d\u7b97\u5b8c\u7f8e\uff0c\u591a\u6b21new\u6784\u9020\u5176\u5b9e\u5e76\u4e0d\u9700\u8981\uff0c\u4e0d\u8fc7\u597d\u5728\u80fd\u591f\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<h4>\u5982\u679c\u662f\u5728axios\u4e2d\uff1f<\/h4>\n<p>\u5982\u679c\u9879\u76ee\u7528\u7684\u662faxios\uff0c\u5219\u7528\u6cd5\u5176\u5b9e\u662f\u7c7b\u4f3c\u7684\uff0c\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>const controller = new AbortController();\r\n\r\naxios.get('\/foo\/bar', {\r\n   signal: controller.signal\r\n}).then(function(response) {\r\n   \/\/...\r\n});\r\n\/\/ \u53d6\u6d88\u5f53\u524d\u8bf7\u6c42\r\ncontroller.abort()<\/pre>\n<p>\u4e07\u53d8\u4e0d\u79bb\u5176\u5b97\uff0c\u5927\u5bb6\u82e5\u662f\u9047\u5230\u7c7b\u4f3c\u9700\u6c42\uff0c\u4e5f\u53ef\u4ee5\u6309\u7167\u4e0a\u9762\u7684\u9aa8\u67b6\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<h4>\u517c\u5bb9\u6027<\/h4>\n<p><code>AbortController<\/code>\u548c<code>AbortSignal<\/code>\u6240\u6709\u73b0\u4ee3\u6d4f\u89c8\u5668\u5747\u652f\u6301\uff0c\u6211\u770b\u4e86\u4e0b\uff0c\u5e94\u8be5\u662f2018\u5e74\u5f00\u59cb\u652f\u6301\u7684\uff0c\u7b97\u7b97\u5230\u73b0\u5728\uff0c\u5e94\u8be5\u53ef\u4ee5\u5728\u6b63\u5f0f\u73af\u5883\u4e2d\u4f7f\u7528\u4e86\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u653e\u5fc3\uff0c\u8fd8\u53ef\u4ee5\u518d\u5f15\u5165\u4e00\u6bb5polyfill\u517c\u5bb9\u4f4e\u7248\u672c\u7684\u6d4f\u89c8\u5668\uff0c\u89c1\u8fd9\u4e2a\u9879\u76ee\uff1a<a href=\"https:\/\/github.com\/mo\/abortcontroller-polyfill\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/mo\/abortcontroller-polyfill<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202301\/2023-01-17_230337.png\" width=\"655\" height=\"344\" alt=\"AbortController\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e09\u3001\u6625\u8282\u5feb\u4e50<\/h3>\n<p>\u8fd9\u5e94\u8be5\u662f\u6625\u8282\u524d\u7684\u6700\u540e\u4e00\u7bc7\u4e86\uff0c\u8fd8\u6709\u4e00\u7bc7\u5199\u4e86\u4e00\u534a\uff0c\u6ca1\u65f6\u95f4\u66f4\u65b0\u4e86\uff0c\u56e0\u4e3a\u660e\u5929\u4e0b\u5348\u5c31\u4f1a\u56de\u8001\u5bb6\u4e86\uff0c\u6211\u8981\u53bb\u9493\u9c7c\u4e86\uff0c\u563f\u563f\u3002<\/p>\n<p>\u73b0\u5728\u603b\u7b97\u6765\u53bb\u81ea\u7531\u4e86\u3002<\/p>\n<p>2022\u5e74\u4e00\u6643\u5c31\u8fc7\u53bb\u4e86\uff0c\u8fd9\u4e00\u5468\u5468\u7684\uff0c\u8d8a\u5e74\u957f\uff0c\u65f6\u95f4\u8fc7\u5f97\u8d8a\u5feb\u3002<\/p>\n<p>\u6765\u5e74\u7ee7\u7eed\u52a0\u6cb9\u5427\uff0c\u9493\u9c7c\u6280\u672f\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\uff0c\u6211\u89c9\u5f97\u660e\u5e74\u4f1a\u662f\u9493\u9c7c\u6c34\u5e73\u957f\u8db3\u8fdb\u6b65\u7684\u4e00\u5e74\u3002<\/p>\n<p>\u5982\u679c\u672c\u6587\u5185\u5bb9\u4f60\u89c9\u5f97\u6709\u6240\u5b66\uff0c\u6b22\u8fce\u8f6c\u53d1\uff0c\u6b22\u8fce\u5206\u4eab\u3002<\/p>\n<p>???<\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10694\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10694<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202301\/abort-cover.jpg\" width=\"160\" height=\"101\" alt=\"s\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u4eca\u5929\u624d\u77e5\u9053\uff0c\u539f\u6765\u6d4f\u89c8\u5668\u63d0\u4f9b\u4e86\u539f\u751f\u7684API\u7528\u6765abort\u963b\u65adfetch\u8bf7\u6c42\uff08\u6216\u8005 axios \u8fd9\u4e2aJS\u5e93\u53d1\u51fa\u7684\u8bf7\u6c42\uff09\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1891,1893,214,1892,1148,903,875],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10694"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=10694"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10694\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=10694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=10694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=10694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}