{"id":34165,"date":"2024-11-25T14:40:13","date_gmt":"2024-11-25T06:40:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34165\/"},"modified":"2024-11-25T14:40:13","modified_gmt":"2024-11-25T06:40:13","slug":"%e5%b0%8f%e7%a8%8b%e5%ba%8f%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bc%98%e9%9b%85%e7%9a%84%e6%8d%95%e6%8d%89%e5%bc%82%e6%ad%a5%e6%96%b9%e6%b3%95%e7%9a%84%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34165\/","title":{"rendered":"\u5c0f\u7a0b\u5e8f\u4e2d\u5982\u4f55\u4f18\u96c5\u7684\u6355\u6349\u5f02\u6b65\u65b9\u6cd5\u7684\u5f02\u5e38"},"content":{"rendered":"<p>\u539f\u751f\u5c0f\u7a0b\u5e8f\u4e2d\u5982\u4f55\u4f18\u96c5\u7684\u6355\u6349\u5f02\u6b65\u65b9\u6cd5\u7684\u5f02\u5e38\uff1f\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e00\u4e0b\u5c0f\u7a0b\u5e8f\u4e2d\u4f18\u96c5\u7684\u4f7f\u7528async await\u5f02\u6b65\u7f16\u7a0b\u7684\u65b9\u6cd5\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/024\/61c2882f0cf69352.jpg\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u4e2d\u5982\u4f55\u4f18\u96c5\u7684\u6355\u6349\u5f02\u6b65\u65b9\u6cd5\u7684\u5f02\u5e38\u63d2\u56fe\" alt=\"\u5c0f\u7a0b\u5e8f\u4e2d\u5982\u4f55\u4f18\u96c5\u7684\u6355\u6349\u5f02\u6b65\u65b9\u6cd5\u7684\u5f02\u5e38\u63d2\u56fe\" \/><\/p>\n<p>\u6700\u8fd1\u5f00\u59cb\u5199\u4e00\u4e2a\u5f00\u6e90\u7684\u4e91\u5f00\u53d1\u793e\u533a\u5c0f\u7a0b\u5e8f\uff0c\u5728\u5199\u7684\u8fc7\u7a0b\u4e2d\u6162\u6162\u7684\u6478\u7d22\u4e86\u4e00\u4e9b\u5bf9\u4e8e\u5f00\u53d1\u548c\u6d4b\u8bd5\u7684\u8fc7\u7a0b\u4e2d\u53c8\u5e2e\u52a9\u7684\u5947\u6280\u6deb\u5de7\u3002\u8fd9\u7bc7\u6587\u7ae0\u8bb2\u4e00\u4e0b\u5728 <strong>\u539f\u751f\u5c0f\u7a0b\u5e8f\u4e2d\u5982\u4f55\u4f18\u96c5\u7684\u6355\u6349\u5f02\u6b65\u65b9\u6cd5\u7684\u5f02\u5e38<\/strong>\u3002<\/p>\n<h2>\u4f20\u7edf\u65b9\u6cd5<\/h2>\n<p>\u5728ES7\u4e4b\u540e\uff0c\u6211\u4eec\u5f80\u5f80\u4f7f\u7528 async await \u8bed\u6cd5\u6765\u8fdb\u884c\u5f02\u6b65\u7f16\u7a0b\uff0c\u5982\u679c\u6211\u4eec\u8981\u6355\u6349\u5f02\u5e38\u7684\u8bdd\u4e00\u822c\u6709\u4ee5\u4e0b\u4e24\u79cd\u65b9\u5f0f<\/p>\n<p><strong><span style=\"font-size: 16px\">try catch<\/span><\/strong><\/p>\n<pre>async&nbsp;func(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/do&nbsp;something\n}\ntry&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;res&nbsp;=&nbsp;await&nbsp;func()\n}&nbsp;catch&nbsp;(error)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/handle&nbsp;error\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u9996\u5148\u662f try catch \u6355\u6349\u5f02\u5e38\uff0c\u4f7f\u7528 try catch \u786e\u5b9e\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u53bb\u5904\u7406\u5f02\u5e38\uff0c\u4e5f\u53ef\u4ee5\u963b\u6b62\u540e\u9762\u65b9\u6cd5\u7684\u8fdb\u884c\uff0c\u4f46\u662f\u5728\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\u54b1\u4eec\u5e38\u5e38\u4e0d\u6b62\u4e00\u4e2a\u5f02\u6b65\u65b9\u6cd5\uff0c\u5927\u91cf\u4f7f\u7528 try catch \u4e0d\u4ec5\u5199\u7684\u4e0d\u723d\uff0c\u4e5f\u7edd\u5bf9\u7b97\u4e0d\u4e0a\u4f18\u96c5\u3002<\/p>\n<p><strong><span style=\"font-size: 16px\">Promise.catch()<\/span><\/strong><\/p>\n<pre>async&nbsp;func(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/do&nbsp;something\n}\n\nconst&nbsp;res&nbsp;=&nbsp;await&nbsp;func().catch(error=&amp;gt;{\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/handle&nbsp;error\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>Promise\u5bf9\u8c61\u5185\u90e8\u4e86try catch ,\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u94fe\u5f0f\u8c03\u7528\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5f02\u5e38\u3002\u76f8\u6bd4try catch\uff0cPromise.catch() \u5199\u8d77\u6765\u5f53\u7136\u662f\u723d\u5f88\u591a\u7684\uff0c\u770b\u7740\u4e5f\u6bd4\u8f83\u4f18\u96c5\u4e86\u3002<\/p>\n<p>\u4f46\u662f\u5f53\u6211\u4eec\u60f3\u8981\u5728\u6355\u6349\u5230\u9519\u8bef\u540e\u505c\u6b62\u65b9\u6cd5\u7684\u7ee7\u7eed\u6267\u884c\uff0c\u90a3\u4e48 Promise.catch() \u5c31\u6ca1\u529e\u6cd5\u505a\u5230\u4e86\uff0c\u5982\u4e0b\u4f8b\u5b50<\/p>\n<pre>async&nbsp;func(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/do&nbsp;something\n}\n\nconst&nbsp;res&nbsp;=&nbsp;await&nbsp;func().catch(error=&amp;gt;{\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5373\u4f7freturn\u4e5f\u65e0\u6548\n&nbsp;&nbsp;&nbsp;&nbsp;return\n})\n\/\/&nbsp;\u5982\u679c\u6709\u9519\u8bef\u7684\u8bdd\u6211\u5c31\u4e0d\u6267\u884c\u4e86<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2>\u4f18\u96c5\u65b9\u5f0f<\/h2>\n<blockquote><p>\n  await-to-js github\u94fe\u63a5https:\/\/github.com\/scopsy\/await-to-js\n<\/p><\/blockquote>\n<p>await-to-js \u8fd9\u4e2a\u5e93\u5e94\u8be5\u5f88\u591a\u4eba\u4e86\u89e3\u8fc7\u4e86\uff0c\u5b83\u662f\u4e00\u4e2a\u5f02\u6b65\u8bf7\u6c42\u7684\u5305\u88c5\u5668\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u8bf7\u6c42\u7684\u9519\u8bef\uff0c\u6839\u636e\u6211\u4eec\u4e0a\u9762\u7684\u9700\u6c42\u6539\u9020\u4e3a\u4f7f\u7528 await-to-js \u7684\u4f8b\u5b50\u5982\u4e0b<\/p>\n<pre>import&nbsp;to&nbsp;from&nbsp;'await-to-js';\n\nasync&nbsp;func(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/do&nbsp;something\n}\n\nconst&nbsp;[err,res]&nbsp;=&nbsp;await&nbsp;to(func())\nif(err){\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/handle&nbsp;error\n&nbsp;&nbsp;&nbsp;&nbsp;return\n}\n\/\/&nbsp;\u5982\u679c\u6709\u9519\u8bef\u7684\u8bdd\u6211\u5c31\u4e0d\u6267\u884c\u4e86<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u5c06\u6211\u4eec\u7684\u5f02\u6b65\u65b9\u6cd5\u4f5c\u4e3a to() \u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u8fd4\u56de\u503c\u901a\u8fc7<strong>\u4e00\u4e2a\u6570\u7ec4\u89e3\u6784\u83b7\u53d6<\/strong>\uff0c\u6570\u7ec4\u7b2c\u4e00\u4e2a\u503c\u4e3a\u6355\u6349\u5230\u7684\u9519\u8bef\uff0c\u7b2c\u4e8c\u4e2a\u503c\u4e3a\u6b63\u5e38\u6267\u884c\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<p>await-to-js\u7684\u5b9e\u73b0\u539f\u7406\u4e5f\u975e\u5e38\u7b80\u5355\uff0c\u5c31\u662f\u4f7f\u7528 Promise.catch()\u83b7\u53d6\u5230\u5f02\u5e38\u540e\u518d\u5c06\u7ed3\u679c\u8fd4\u56de\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c \u6e90\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre>export&nbsp;function&nbsp;to&lt;t&gt;&nbsp;(\n\tpromise:&nbsp;Promise&lt;t&gt;,\n\terrorExt?:&nbsp;object\n&nbsp;&nbsp;):&nbsp;Promise&nbsp;{\n\treturn&nbsp;promise\n\t&nbsp;&nbsp;.then((data:&nbsp;T)&nbsp;=&amp;gt;&nbsp;[null,&nbsp;data])\n\t&nbsp;&nbsp;.catch((err:&nbsp;U)&nbsp;=&amp;gt;&nbsp;{\n\t\tif&nbsp;(errorExt)&nbsp;{\n\t\t&nbsp;&nbsp;const&nbsp;parsedError&nbsp;=&nbsp;Object.assign({},&nbsp;err,&nbsp;errorExt);\n\t\t&nbsp;&nbsp;return&nbsp;[parsedError,&nbsp;undefined];\n\t\t}\n&nbsp;&nbsp;\n\t\treturn&nbsp;[err,&nbsp;undefined];\n\t&nbsp;&nbsp;});\n&nbsp;&nbsp;}\n&nbsp;&nbsp;\n&nbsp;&nbsp;export&nbsp;default&nbsp;to;&lt;\/t&gt;&lt;\/t&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2>\u5c0f\u7a0b\u5e8f\u4e2d\u4f7f\u7528<\/h2>\n<p>\u5728\u5c0f\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528npm\u5305\u5e76\u4e0d\u65b9\u4fbf\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5c06\u6e90\u7801\u62ff\u51fa\u6765\u5355\u72ec\u4f7f\u7528\uff0c\u518d\u52a0\u4ee5\u6539\u9020\u7684\u8bdd\u5c31\u53ef\u4ee5\u4f18\u96c5\u7684\u8fdb\u884c\u5f02\u6b65\u7f16\u7a0b\u5566\uff0c\u6211\u81ea\u5df1\u7684\u6539\u9020\u65b9\u5f0f\u5982\u4e0b\u3002<\/p>\n<pre>\/\/&nbsp;lib\/awaitTo.js\nmodule.exports&nbsp;=&nbsp;function&nbsp;to(promise,&nbsp;description=\"unknown\")&nbsp;{\n\tconst&nbsp;pages&nbsp;=&nbsp;getCurrentPages()\n\tconst&nbsp;route&nbsp;=&nbsp;pages[pages.length&nbsp;-&nbsp;1].route||'unknown'\n\tdescription&nbsp;=&nbsp;`[${route}]---[${description}]`\n\tconsole.time(description)\n\treturn&nbsp;promise\n\t\t.then(function&nbsp;(data)&nbsp;{\n\t\t\tconsole.timeEnd(description)\n\t\t\treturn&nbsp;[null,&nbsp;data];\n\t\t})\n\t\t.catch(function&nbsp;(err)&nbsp;{\n\t\t\twx.showToast({\n\t\t\t\ttitle:&nbsp;'\u8bf7\u6c42\u5931\u8d25',\n\t\t\t\ticon:&nbsp;\"none\"\n\t\t\t})\n\t\t\treturn&nbsp;[err,&nbsp;undefined];\n\t\t});\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u901a\u8fc7 getCurrentPages() \u7684\u65b9\u5f0f\u83b7\u53d6\u5230\u5f02\u6b65\u65b9\u6cd5\u6267\u884c\u65f6\u5bf9\u5e94\u7684\u9875\u9762\u8def\u7531\uff0c\u5728\u5c06\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6539\u4e3a\u81ea\u5df1<strong>\u5bf9\u5f02\u6b65\u65b9\u6cd5\u7684\u4e00\u4e2a\u63cf\u8ff0<\/strong>\uff0c\u6bcf\u4e00\u6b21\u8c03\u7528\u5f02\u6b65\u65b9\u6cd5\u7684\u65f6\u5019\u5c31\u4f1a\u5728 <strong>\u63a7\u5236\u53f0\u8f93\u51fa\u6267\u884c\u65f6\u95f4<\/strong>\u3002 \u5b9e\u9645\u4f7f\u7528\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a<\/p>\n<pre>const&nbsp;to&nbsp;=&nbsp;require(\"..\/..\/lib\/awaitTo\")\nconst&nbsp;[err,&nbsp;res]&nbsp;=&nbsp;await&nbsp;to(db.collection(\"post\").add({\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data:&nbsp;form\n}),\"addPost\")\nif&nbsp;(err)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5904\u7406\u6211\u7684\u9519\u8bef\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return\n}\n\/\/&nbsp;\u6210\u529f\u540e\u6267\u884c\u7684\u903b\u8f91<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u63a7\u5236\u53f0\u6253\u5370\u7684\u6267\u884c\u65f6\u95f4\u8f93\u51fa\u5982\u4e0b,\u6253\u5370\u683c\u5f0f\u662f<\/p>\n<blockquote>\n<p>\u3010\u8def\u7531\u9875\u9762\u3011&#8212;\u3010\u65b9\u6cd5\u63cf\u8ff0\u3011\uff1a\u6267\u884c\u65f6\u95f4<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/image\/870\/165\/149\/1640138554896180.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u4e2d\u5982\u4f55\u4f18\u96c5\u7684\u6355\u6349\u5f02\u6b65\u65b9\u6cd5\u7684\u5f02\u5e38\u63d2\u56fe1\" alt=\"\u5c0f\u7a0b\u5e8f\u4e2d\u5982\u4f55\u4f18\u96c5\u7684\u6355\u6349\u5f02\u6b65\u65b9\u6cd5\u7684\u5f02\u5e38\u63d2\u56fe1\" \/><\/p>\n<p>\u5f53\u7136\u6211\u7684\u5c01\u88c5\u65b9\u5f0f\u662f\u9488\u5bf9\u6211\u81ea\u5df1\u7684\u4ee3\u7801\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u5b9e\u9645\u4e1a\u52a1\u518d\u8fdb\u884c\u6539\u9020\uff0c\u4f8b\u5982\u57cb\u70b9\u7684\u7edf\u4e00\u5165\u53e3\u4e4b\u7c7b\u7684\uff0c\u53ef\u4ee5\u6781\u5927\u63d0\u5347\u5f00\u53d1\u6548\u7387\uff01<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u5728\u4f7f\u7528\u4e91\u5f00\u53d1\u65f6\uff0c\u4e3a\u4e86\u5f00\u53d1\u4f53\u9a8c\u4e0d\u5f97\u4e0d\u4f7f\u7528\u539f\u751f\u7684\u8bed\u8a00\u53bb\u5199\uff0c\u76f8\u5bf9\u95ed\u585e\u7684\u73af\u5883\u5c31\u4f7f\u5f97\u5f88\u591aweb\u7aef\u7684\u65b9\u6cd5\u6ca1\u6cd5\u4f7f\u7528\uff0c\u5728\u8fd1\u671f\u7684\u6478\u7d22\u4e2d\u6211\u4e5f\u627e\u5230\u4e86\u5f88\u591a\u63d0\u5347\u539f\u751f\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4f53\u9a8c\u7684\u65b9\u5f0f\uff0c\u4e4b\u540e\u5c06\u4f1a\u9646\u7eed\u66f4\u65b0\u3002<\/p>\n<p>\u3010\u76f8\u5173\u5b66\u4e60\u63a8\u8350\uff1a\u3011<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5c0f\u7a0b\u5e8f\u4e2d\u5982\u4f55\u4f18\u96c5\u7684\u6355\u6349\u5f02\u6b65\u65b9\u6cd5\u7684\u5f02\u5e38\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>\u539f\u751f\u5c0f\u7a0b\u5e8f\u4e2d\u5982\u4f55\u4f18\u96c5\u7684\u6355\u6349\u5f02\u6b65\u65b9\u6cd5\u7684\u5f02\u5e38\uff1f\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e00\u4e0b\u5c0f\u7a0b\u5e8f\u4e2d\u4f18\u96c5\u7684\u4f7f\u7528async await\u5f02\u6b65\u7f16\u7a0b\u7684\u65b9\u6cd5\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\uff01 \u6700\u8fd1\u5f00\u59cb\u5199\u4e00\u4e2a\u5f00\u6e90\u7684\u4e91\u5f00\u53d1\u793e\u533a\u5c0f\u7a0b\u5e8f\uff0c\u5728\u5199\u7684\u8fc7\u7a0b\u4e2d\u6162\u6162\u7684\u6478\u7d22\u4e86\u4e00\u4e9b\u5bf9\u4e8e\u5f00\u53d1\u548c\u6d4b\u8bd5\u7684\u8fc7\u7a0b\u4e2d\u53c8\u5e2e\u52a9\u7684\u5947\u6280\u6deb\u5de7\u3002\u8fd9\u7bc7\u6587\u7ae0\u8bb2\u4e00\u4e0b\u5728 \u539f\u751f\u5c0f\u7a0b\u5e8f\u4e2d\u5982\u4f55\u4f18\u96c5\u7684\u6355\u6349\u5f02\u6b65\u65b9\u6cd5\u7684\u5f02\u5e38\u3002 \u4f20\u7edf\u65b9\u6cd5 \u5728ES7\u4e4b\u540e\uff0c\u6211\u4eec\u5f80\u5f80\u4f7f\u7528 async await \u8bed\u6cd5\u6765\u8fdb\u884c\u5f02\u6b65\u7f16\u7a0b\uff0c\u5982\u679c\u6211\u4eec\u8981\u6355\u6349\u5f02\u5e38\u7684\u8bdd\u4e00\u822c\u6709\u4ee5\u4e0b\u4e24\u79cd\u65b9\u5f0f try catch async&nbsp;func(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/do&nbsp;something } try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;res&nbsp;=&nbsp;await&nbsp;func() }&nbsp;catch&nbsp;(error)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;\/\/handle&nbsp;error } \u767b\u5f55\u540e\u590d\u5236 \u9996\u5148\u662f try catch \u6355\u6349\u5f02\u5e38\uff0c\u4f7f\u7528 try catch \u786e\u5b9e\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u53bb\u5904\u7406\u5f02\u5e38\uff0c\u4e5f\u53ef\u4ee5\u963b\u6b62\u540e\u9762\u65b9\u6cd5\u7684\u8fdb\u884c\uff0c\u4f46\u662f\u5728\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\u54b1\u4eec\u5e38\u5e38\u4e0d\u6b62\u4e00\u4e2a\u5f02\u6b65\u65b9\u6cd5\uff0c\u5927\u91cf\u4f7f\u7528 try catch \u4e0d\u4ec5\u5199\u7684\u4e0d\u723d\uff0c\u4e5f\u7edd\u5bf9\u7b97\u4e0d\u4e0a\u4f18\u96c5\u3002 Promise.catch() async&nbsp;func(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/do&nbsp;something } const&nbsp;res&nbsp;=&nbsp;await&nbsp;func().catch(error=&amp;gt;{ &nbsp;&nbsp;&nbsp;&nbsp;\/\/handle&nbsp;error }) \u767b\u5f55\u540e\u590d\u5236 Promise\u5bf9\u8c61\u5185\u90e8\u4e86try catch ,\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u94fe\u5f0f\u8c03\u7528\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5f02\u5e38\u3002\u76f8\u6bd4try catch\uff0cPromise.catch() \u5199\u8d77\u6765\u5f53\u7136\u662f\u723d\u5f88\u591a\u7684\uff0c\u770b\u7740\u4e5f\u6bd4\u8f83\u4f18\u96c5\u4e86\u3002 \u4f46\u662f\u5f53\u6211\u4eec\u60f3\u8981\u5728\u6355\u6349\u5230\u9519\u8bef\u540e\u505c\u6b62\u65b9\u6cd5\u7684\u7ee7\u7eed\u6267\u884c\uff0c\u90a3\u4e48 Promise.catch() \u5c31\u6ca1\u529e\u6cd5\u505a\u5230\u4e86\uff0c\u5982\u4e0b\u4f8b\u5b50 async&nbsp;func(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/do&nbsp;something } const&nbsp;res&nbsp;=&nbsp;await&nbsp;func().catch(error=&amp;gt;{ &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5373\u4f7freturn\u4e5f\u65e0\u6548 &nbsp;&nbsp;&nbsp;&nbsp;return }) \/\/&nbsp;\u5982\u679c\u6709\u9519\u8bef\u7684\u8bdd\u6211\u5c31\u4e0d\u6267\u884c\u4e86 \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-34165","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34165","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=34165"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34165\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}