{"id":1247,"date":"2024-11-10T17:54:26","date_gmt":"2024-11-10T09:54:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1247\/"},"modified":"2024-11-10T17:54:26","modified_gmt":"2024-11-10T09:54:26","slug":"%e5%ae%89%e5%85%a8%e8%b5%8b%e5%80%bc%e8%bf%90%e7%ae%97%e7%ac%a6%ef%bc%9atry-catch-%e7%9a%84%e6%9b%bf%e4%bb%a3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1247\/","title":{"rendered":"\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1atry\/catch \u7684\u66ff\u4ee3\u65b9\u6848"},"content":{"rendered":"<h2> \u6ce8\u610f\u529b <\/h2>\n<p><strong>\u6211\u60f3\u6307\u51fa\uff0c\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26\u5c1a\u672a\u5305\u542b\u5728 ecmascript \u6807\u51c6\u4e2d\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528 ecmascript \u6807\u51c6\u4e2d\u7684\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002<\/strong><\/p>\n<hr>\n<p>\u5982\u679c\u4f60\u559c\u6b22\u6211\u7684\u6587\u7ae0\uff0c\u53ef\u4ee5\u8bf7\u6211\u4e00\u676f\u5496\u5561:)<br \/><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173034094164602.jpg\" class=\"aligncenter\" title=\"\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1atry\/catch \u7684\u66ff\u4ee3\u65b9\u6848\u63d2\u56fe\" alt=\"\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1atry\/catch \u7684\u66ff\u4ee3\u65b9\u6848\u63d2\u56fe\" \/><\/p>\n<hr>\n<p>\u5728\u4f7f\u7528 <strong>javascript<\/strong> \u5f00\u53d1\u65f6\uff0c\u7279\u522b\u662f\u5728\u4f7f\u7528\u5f02\u6b65\u51fd\u6570\u6216\u590d\u6742 api \u65f6\uff0c\u6211\u4eec\u4f7f\u7528 <strong>try\/catch<\/strong> \u8fdb\u884c\u9519\u8bef\u5904\u7406\u3002 <strong>ecmascript<\/strong> \u5ba3\u5e03\u4e86\u4e00\u4e2a\u65b0\u7684\u8fd0\u7b97\u7b26\u3002 <strong>\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26<\/strong> (?=) \u7b80\u5316\u4e86\u9519\u8bef\u5904\u7406\uff0c\u5e76\u4e3a\u5904\u7406\u7ed3\u679c\u548c\u9519\u8bef\u63d0\u4f9b\u4e86\u7edf\u4e00\u7684\u7ed3\u6784\uff0c\u5c24\u5176\u662f\u5728\u5f02\u6b65\u64cd\u4f5c\u671f\u95f4\u3002<\/p>\n<p>\u5f53\u60a8\u4f7f\u7528<strong>\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26<\/strong>\u65f6\uff0c\u5b83\u4f1a\u68c0\u67e5\u51fd\u6570\u6216\u64cd\u4f5c\u662f\u5426\u6210\u529f\u3002\u5982\u679c\u6210\u529f\uff0c\u5219\u8fd4\u56de\u7ed3\u679c\u3002\u5982\u679c\u4e0d\u6210\u529f\u5219\u8fd4\u56de\u9519\u8bef\u3002<\/p>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u63a2\u7d22\u5982\u4f55\u4f7f\u7528<strong>\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26(?=)<\/strong>\u3002<\/p>\n<hr>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u7f16\u5199\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684 api \u8bf7\u6c42\u7684<strong>\u8bed\u6cd5<\/strong>\u3002<\/p>\n<pre>async function fetchData() {\n  const [error, response] ?= await fetch(\"https:\/\/jsonplaceholder.typicode.com\/todos\/1\"); \/\/ safe assigment op.\n\n  if (error) {\n    console.error(\"Fetch error:\", error);\n    return;\n  }\n\n  const [jsonError, jsonData] ?= await response.json();\n\n  if (jsonError) {\n    console.error(\"JSON error:\", jsonError);\n    return;\n  }\n\n  return jsonData;\n}\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728<strong>api\u8bf7\u6c42<\/strong>\u4e2d\uff0c\u6bcf\u4e00\u6b65\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\uff1b\u8be5\u5bf9\u8c61\u5b58\u50a8\u4e0e<strong>\u8bf7\u6c42<\/strong>\u76f8\u5173\u7684\u4fe1\u606f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<strong>\u5bf9\u8c61\u89e3\u6784<\/strong>\u529f\u80fd\u6765\u83b7\u53d6\u8be5\u5bf9\u8c61\u5185\u90e8\u7684\u6570\u636e\u3002\u501f\u52a9<strong>\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26<\/strong>\uff08?=\uff09\uff0c\u5f53<strong>api\u8bf7\u6c42<\/strong>\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u89e3\u6784\u65b9\u6cd5\u5904\u7406\u9519\u8bef\u5bf9\u8c61\uff0c\u5b89\u5168\u5730\u68c0\u7d22\u9519\u8bef\u6d88\u606f\u6216\u9519\u8bef\u4ee3\u7801\u3002 const [\u9519\u8bef\uff0c\u54cd\u5e94]\uff0c<strong>\u9519\u8bef\u8fd4\u56de\u9519\u8bef<\/strong>\u3002 <strong>\u54cd\u5e94\u8fd4\u56de\u6570\u636e<\/strong><\/p>\n<hr>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u7814\u7a76\u4e86\u5982\u4f55\u5728 <strong>ecmascript \u6807\u51c6<\/strong> \u4e2d\u4f7f\u7528<strong>\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26<\/strong>\u3002\u6211\u8ba4\u4e3a\u4ed6\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u64cd\u4f5c\u5458\u3002\u73b0\u5728\u6211\u4eec\u77e5\u9053\u5982\u4f55\u4f7f\u7528\u5b83\u4e86\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1atry\/catch \u7684\u66ff\u4ee3\u65b9\u6848\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\u610f\u529b \u6211\u60f3\u6307\u51fa\uff0c\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26\u5c1a\u672a\u5305\u542b\u5728 ecmascript \u6807\u51c6\u4e2d\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528 ecmascript \u6807\u51c6\u4e2d\u7684\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002 \u5982\u679c\u4f60\u559c\u6b22\u6211\u7684\u6587\u7ae0\uff0c\u53ef\u4ee5\u8bf7\u6211\u4e00\u676f\u5496\u5561:) \u5728\u4f7f\u7528 javascript \u5f00\u53d1\u65f6\uff0c\u7279\u522b\u662f\u5728\u4f7f\u7528\u5f02\u6b65\u51fd\u6570\u6216\u590d\u6742 api \u65f6\uff0c\u6211\u4eec\u4f7f\u7528 try\/catch \u8fdb\u884c\u9519\u8bef\u5904\u7406\u3002 ecmascript \u5ba3\u5e03\u4e86\u4e00\u4e2a\u65b0\u7684\u8fd0\u7b97\u7b26\u3002 \u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26 (?=) \u7b80\u5316\u4e86\u9519\u8bef\u5904\u7406\uff0c\u5e76\u4e3a\u5904\u7406\u7ed3\u679c\u548c\u9519\u8bef\u63d0\u4f9b\u4e86\u7edf\u4e00\u7684\u7ed3\u6784\uff0c\u5c24\u5176\u662f\u5728\u5f02\u6b65\u64cd\u4f5c\u671f\u95f4\u3002 \u5f53\u60a8\u4f7f\u7528\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26\u65f6\uff0c\u5b83\u4f1a\u68c0\u67e5\u51fd\u6570\u6216\u64cd\u4f5c\u662f\u5426\u6210\u529f\u3002\u5982\u679c\u6210\u529f\uff0c\u5219\u8fd4\u56de\u7ed3\u679c\u3002\u5982\u679c\u4e0d\u6210\u529f\u5219\u8fd4\u56de\u9519\u8bef\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u63a2\u7d22\u5982\u4f55\u4f7f\u7528\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26(?=)\u3002 \u73b0\u5728\u8ba9\u6211\u4eec\u7f16\u5199\u5b89\u5168\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684 api \u8bf7\u6c42\u7684\u8bed\u6cd5\u3002 async function fetchData() { const [error, response] ?= await fetch(&#8220;https:\/\/jsonplaceholder.typicode.com\/todos\/1&#8221;); \/\/ safe assigment op. if (error) { console.error(&#8220;Fetch error:&#8221;, error); return; } const [jsonError, jsonData] ?= await response.json(); if (jsonError) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1247","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=1247"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1247\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}