{"id":424018,"date":"2021-04-03T20:02:41","date_gmt":"2021-04-03T12:02:41","guid":{"rendered":"http:\/\/4563.org\/?p=424018"},"modified":"2021-04-03T20:02:41","modified_gmt":"2021-04-03T12:02:41","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e8%bf%99%e4%b8%aa-try-catch-finally-%e6%b2%a1%e6%9c%89%e6%b2%a1%e6%9c%89%e6%89%a7%e8%a1%8c-finally-%e9%83%a8%e5%88%86%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=424018","title":{"rendered":"\u4e3a\u4ec0\u4e48\u8fd9\u4e2a try-catch-finally \u6ca1\u6709\u6ca1\u6709\u6267\u884c finally \u90e8\u5206\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e3a\u4ec0\u4e48\u8fd9\u4e2a try-catch-finally \u6ca1\u6709\u6ca1\u6709\u6267\u884c finally \u90e8\u5206\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xialvjun <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>import got from 'got'; (async () =&gt; {   try {     console.log('try');     const res = await got('https:\/\/www.baidu.com');     console.log('got', res);   } catch (error) {     console.log('error', error);   } finally {     console.log('finally');   } })(); <\/code><\/pre>\n<p>\u76f4\u63a5\u8f93\u51fa<\/p>\n<pre><code>[email&#160;protected]:~\/c\/test_got \u27a4 ts-node index.ts try [email&#160;protected]:~\/c\/test_got \u27a4  <\/code><\/pre>\n<p>got: &#8220;version&#8221;: &#8220;11.8.2&#8221;<\/p>\n<p>\u7136\u540e\uff0c\u5728\u4e0b\u9762\u52a0\u4e86\u4e2a setTimeout \u907f\u514d nodejs \u9000\u51fa\uff08\u6211\u4e5f\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\u5c31\u76f4\u63a5\u9000\u51fa\u4e86\uff09\u4e5f\u6ca1\u7528\u3002<\/p>\n<pre><code>import got from 'got'; (async () =&gt; {   try {     console.log('try');     const res = await got('https:\/\/www.baidu.com');     console.log('got', res);   } catch (error) {     console.log('error', error);   } finally {     console.log('finally');   } })();  console.log('waiting'); setTimeout(() =&gt; {   console.log('wait over'); }, 1e9); <\/code><\/pre>\n<p>\u8f93\u51fa<\/p>\n<pre><code>[email&#160;protected]:~\/c\/test_got \u27a4 ts-node index.ts try waiting   <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>15<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5629198\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xialvjun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5df2\u7ecf 59 \u6b21\u70b9\u51fb\u4e86\uff0c\u4e3a\u4ec0\u4e48\u90fd\u6ca1\u4eba\u56de\u590d\u5462\uff1f\u96be\u9053\u8fd9\u95ee\u9898\u592a\u7b80\u5355\u4e86\uff1f\u6211\u662f\u6709\u4ec0\u4e48\u57fa\u7840\u70b9\u6ca1\u5b66\u5230\u4f4d\u5417\uff1f\uff08\u6000\u7591\u4eba\u751f\u4e2d\uff09\u3002<br \/>\u597d\u5947\u8fd9\u95ee\u9898\uff0c\u5982\u679c\u4e5f\u662f\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\u7684\uff0c\u4e5f\u56de\u590d\u4e00\u4e0b\u554a\u3002\u81f3\u5c11\u8ba9\u6211\u77e5\u9053\u4e0d\u662f\u6211\u4e00\u4e2a\u4eba\u83dc\u554a\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629199\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vegetable <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xialvjun <br \/>\u662f\u56e0\u4e3a\u590d\u73b0\u4e0d\u4e86\u4f60\u7684\u95ee\u9898\u3002<\/p>\n<p>\u9996\u884c\u6539\u6210 const got = require(&#8220;got&#8221;);\u4e4b\u540e\u7528 node \u6ca1\u884c\uff0c\u4e13\u95e8\u88c5\u4e86\u4e00\u4e2a ts-node\uff0c\u4e5f\u590d\u73b0\u4e0d\u4e86\u3002<\/p>\n<p>\u90fd\u80fd\u6b63\u5e38\u6253\u5370 finally<\/p>\n<p>\u7ed9\u7684\u8f93\u51fa\uff0c\u4e3a\u4ec0\u4e48\u6ca1\u6253\u5370\u51fa\u6570\u636e\u6216\u8005 error \uff1f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629200\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xialvjun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5947\u602a\uff0c\u6211\u7528 pnpm \u5b89\u88c5\u7684\u3002\u3002\u3002<br \/>\u6211\u6362\u6362 npm \u770b\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629201\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fy136649111 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             14.5.0 \u7684 node \u548c 8.10.2 \u7684 ts-node \u590d\u73b0\u4e0d\u51fa\u6765                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629202\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : faceRollingKB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u6ca1\u53d1\u73b0\u4f60\u7684\u8f93\u51fa\u65e2\u6ca1\u6709 console.log(&#8216;got&#8217;, res);\u53c8\u6ca1\u6709 console.log(&#8216;error&#8217;, error);\u4e48\uff1f\u8fd9\u8bf4\u660e await \u7684\u90e8\u5206\u8fd8\u5728\u7b49\u5f85\u554a\u7b28                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629203\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : libook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;got&#8221;: &#8220;^11.8.2&#8221;,<br \/>&#8220;ts-node&#8221;: &#8220;^9.1.1&#8221;<br \/>\u6d4b\u8bd5\u6ca1\u6709\u95ee\u9898\u3002<\/p>\n<p>\u4f60\u8fd9\u4e2a try \u6ca1\u6267\u884c\u5b8c\uff0c\u6309\u7406\u8bf4 got \u6267\u884c\u5b8c\u540e\u4f1a\u6709 console.log(&#8216;got&#8217;, res);\u8f93\u51fa\uff0c\u8fd9\u4e2a\u6211\u8fd9\u8fb9\u6d4b\u8bd5\u662f\u80fd\u8f93\u51fa\u4e00\u5927\u4e32\uff0c\u4f60\u6ca1\u6709\u8f93\u51fa\u8fd9\u4e2a\u76f4\u63a5\u9000\u51fa\u4e86\u7a0b\u5e8f\uff0c\u8bf4\u660e\u5728\u6267\u884c const res = await got(&#8216;https:\/\/www.baidu.com&#8217;);\u7684\u65f6\u5019\u5c31\u5bfc\u81f4\u7a0b\u5e8f\u9000\u51fa\u4e86\u3002<\/p>\n<p>\u4e0d\u77e5\u9053\u662f ts-node \u8fd8\u662f got \u7684 bug\uff0c\u4f60\u8fd9\u4e2a\u4ee3\u7801\u5b8c\u5168\u6ca1\u6709\u7528 TS \u7684\u8bed\u6cd5\uff0c\u76f4\u63a5 node .\/index.js \u5c31\u80fd\u6b63\u5e38\u8fd0\u884c\uff0c\u4f60\u8bd5\u8bd5\u76f4\u63a5\u7528 node \u8fd0\u884c\u6709\u6ca1\u6709\u95ee\u9898\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629204\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xialvjun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u4e0a\u4f20\u4e0b git \u4ed3\u5e93\u5427\u3002\u3002\u3002<br \/>\u27a4 node &#8211;version<br \/>v14.10.0<br \/>[email&#160;protected]:~\/c\/test_got<br \/>\u27a4 ts-node &#8211;version<br \/>v9.1.1                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629205\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cuzfinal <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u63a7\u5236\u53f0\u4e0d\u80fd\u8f93\u51fa\u5f02\u6b65\u7ed3\u679c\uff0c\u5728\u6d4f\u89c8\u5668\u91cc\u8bd5\u8bd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629206\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xialvjun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/github.com\/xialvjun\/node_bug_maybe                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629207\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhw2590582 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5b9e\u6d4b\uff0c\u6ca1\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629208\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xialvjun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @libook \u6539\u6210 require \u8bed\u6cd5\uff0c\u7528 node \u6267\u884c\u4e5f\u4e0d\u884c\u3002\u90fd\u662f\u53ea\u8f93\u51fa\u4e2a \u201ctry\u201d \u5c31\u9000\u51fa\u4e86\u3002\u3002\u3002 \u800c\u6362\u6210\u522b\u7684 \u5f02\u6b65\u51fd\u6570\uff0c\u4f8b\u5982 `const delay = () =&gt; new Promise(re =&gt; setTimeout(res, 1000))` \u5c31\u5b8c\u5168\u6b63\u5e38\u3002\u60f3\u7740\u4f1a\u4e0d\u4f1a\u662f got \u7684 bug\uff0c\u4f46\u662f `grep -RIn process.exit .\/` \u90fd\u6ca1\u53d1\u73b0\u6709\u54ea\u4e2a\u5305\u5199\u4e86 process.exit                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629209\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xialvjun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u90c1\u95f7\uff0c\u6211\u90fd\u8981\u611f\u89c9\u6211\u662f\u4e0d\u662f\u5e94\u8be5\u5f55\u5c4f\u6765\u81ea\u8bc1\u6e05\u767d\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629210\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : libook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xialvjun \u4f60 Node \u5565\u7248\u672c\uff0c\u6211\u662f v15.13.0\uff0c\u4f60\u6362\u4e2a\u7248\u672c\u8bd5\u8bd5\uff1f<\/p>\n<p>\u4f60\u6267\u884c\u5b8c\u811a\u672c\u540e\u7528 echo $? \u6765\u770b\u4e00\u4e0b\u9000\u51fa\u4ee3\u7801\uff0c\u5982\u679c\u662f 0 \u7684\u8bdd\u5c31\u662f\u6b63\u5e38\u9000\u51fa\uff0c\u5982\u679c\u662f\u975e 0 \u7684\u8bdd\u5c31\u8bf4\u660e\u53ef\u80fd\u51fa\u4e86\u5565\u95ee\u9898\u3002<\/p>\n<p>\u4e0d\u7528\u6539 require \u8bed\u6cd5\uff0cnode \u662f\u652f\u6301 ESM \u7684\uff0c\u53ea\u9700\u8981\u5728 package.json \u52a0\u4e0a&#8221;type&#8221;: &#8220;module&#8221;\u5c31\u53ef\u4ee5\u4e86\uff0c\u5f53\u7136\u524d\u63d0\u662f\u4f60\u5f53\u524d\u7684 node \u7248\u672c\u6bd4\u8f83\u65b0\uff0c\u4e0d\u9700\u8981 flag \u5c31\u53ef\u4ee5\u76f4\u63a5\u7528 ESM \u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629211\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xialvjun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/github.com\/sindresorhus\/got\/issues\/1441<br \/>https:\/\/github.com\/nodejs\/node\/issues\/35116<br \/>\u8279\uff0c\u771f\u5de7\uff0c\u8ba9\u6211\u78b0\u4e0a\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629212\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : libook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e2a\u4eba\u7684\u7ecf\u9a8c\u6765\u770b\uff0cLTS \u6700\u597d\u65f6\u523b\u4fdd\u6301\u6700\u65b0\uff0c\u56e0\u4e3a LTS \u7684\u66f4\u65b0\u90fd\u662f\u589e\u5f3a\u53ef\u9760\u6027\u548c\u5b89\u5168\u6027\u7684\u66f4\u65b0\u3002<\/p>\n<p>\u4e0d\u8fc7\u6211\u5728\u516c\u53f8\u6838\u5fc3\u9879\u76ee\u4e0a\u4e00\u76f4\u5728\u7528 Current \u7248\u672c\uff0c\u4e5f\u662f\u65f6\u957f\u4f1a\u66f4\u65b0\uff0c\u54c1\u63a7\u505a\u5f97\u8fd8\u4e0d\u9519\uff0c\u5f88\u591a\u5e74\u6765\u6ca1\u9047\u5230\u4ec0\u4e48\u95ee\u9898\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48\u8fd9\u4e2a try-catch-f&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/424018"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=424018"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/424018\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=424018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=424018"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=424018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}