V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xialvjun
V2EX  ›  Node.js

为什么这个 try-catch-finally 没有没有执行 finally 部分?

  •  
  •   xialvjun · 2021-04-01 10:42:45 +08:00 · 2620 次点击
    这是一个创建于 1093 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import got from 'got';
    (async () => {
      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');
      }
    })();
    

    直接输出

    didi@localhost:~/c/test_got
    ➤ ts-node index.ts
    try
    didi@localhost:~/c/test_got
    ➤ 
    

    got: "version": "11.8.2"

    然后,在下面加了个 setTimeout 避免 nodejs 退出(我也不知道为什么就直接退出了)也没用。

    import got from 'got';
    (async () => {
      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(() => {
      console.log('wait over');
    }, 1e9);
    

    输出

    didi@localhost:~/c/test_got
    ➤ ts-node index.ts
    try
    waiting
    
    
    
    15 条回复    2021-04-01 11:40:35 +08:00
    xialvjun
        1
    xialvjun  
    OP
       2021-04-01 10:52:48 +08:00
    已经 59 次点击了,为什么都没人回复呢?难道这问题太简单了?我是有什么基础点没学到位吗?(怀疑人生中)。
    好奇这问题,如果也是不知道为什么的,也回复一下啊。至少让我知道不是我一个人菜啊。
    Vegetable
        2
    Vegetable  
       2021-04-01 10:55:43 +08:00
    @xialvjun
    是因为复现不了你的问题。

    首行改成 const got = require("got");之后用 node 没行,专门装了一个 ts-node,也复现不了。

    都能正常打印 finally

    给的输出,为什么没打印出数据或者 error ?
    xialvjun
        3
    xialvjun  
    OP
       2021-04-01 10:58:13 +08:00
    奇怪,我用 pnpm 安装的。。。
    我换换 npm 看下
    fy136649111
        4
    fy136649111  
       2021-04-01 10:59:25 +08:00
    14.5.0 的 node 和 8.10.2 的 ts-node 复现不出来
    faceRollingKB
        5
    faceRollingKB  
       2021-04-01 10:59:36 +08:00
    你没发现你的输出既没有 console.log('got', res);又没有 console.log('error', error);么?这说明 await 的部分还在等待啊笨
    libook
        6
    libook  
       2021-04-01 11:01:44 +08:00
    "got": "^11.8.2",
    "ts-node": "^9.1.1"
    测试没有问题。

    你这个 try 没执行完,按理说 got 执行完后会有 console.log('got', res);输出,这个我这边测试是能输出一大串,你没有输出这个直接退出了程序,说明在执行 const res = await got('https://www.baidu.com');的时候就导致程序退出了。

    不知道是 ts-node 还是 got 的 bug,你这个代码完全没有用 TS 的语法,直接 node ./index.js 就能正常运行,你试试直接用 node 运行有没有问题。
    xialvjun
        7
    xialvjun  
    OP
       2021-04-01 11:02:08 +08:00
    我上传下 git 仓库吧。。。
    ➤ node --version
    v14.10.0
    didi@localhost:~/c/test_got
    ➤ ts-node --version
    v9.1.1
    cuzfinal
        8
    cuzfinal  
       2021-04-01 11:03:05 +08:00
    控制台不能输出异步结果,在浏览器里试试
    xialvjun
        9
    xialvjun  
    OP
       2021-04-01 11:05:55 +08:00
    zhw2590582
        10
    zhw2590582  
       2021-04-01 11:18:06 +08:00
    实测,没问题
    xialvjun
        11
    xialvjun  
    OP
       2021-04-01 11:18:57 +08:00
    @libook 改成 require 语法,用 node 执行也不行。都是只输出个 “try” 就退出了。。。 而换成别的 异步函数,例如 `const delay = () => new Promise(re => setTimeout(res, 1000))` 就完全正常。想着会不会是 got 的 bug,但是 `grep -RIn process.exit ./` 都没发现有哪个包写了 process.exit
    xialvjun
        12
    xialvjun  
    OP
       2021-04-01 11:19:54 +08:00
    郁闷,我都要感觉我是不是应该录屏来自证清白了。😵
    libook
        13
    libook  
       2021-04-01 11:25:53 +08:00
    @xialvjun 你 Node 啥版本,我是 v15.13.0,你换个版本试试?

    你执行完脚本后用 echo $? 来看一下退出代码,如果是 0 的话就是正常退出,如果是非 0 的话就说明可能出了啥问题。

    不用改 require 语法,node 是支持 ESM 的,只需要在 package.json 加上"type": "module"就可以了,当然前提是你当前的 node 版本比较新,不需要 flag 就可以直接用 ESM 。
    xialvjun
        14
    xialvjun  
    OP
       2021-04-01 11:27:05 +08:00   ❤️ 1
    libook
        15
    libook  
       2021-04-01 11:40:35 +08:00   ❤️ 1
    个人的经验来看,LTS 最好时刻保持最新,因为 LTS 的更新都是增强可靠性和安全性的更新。

    不过我在公司核心项目上一直在用 Current 版本,也是时长会更新,品控做得还不错,很多年来没遇到什么问题。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3394 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 10:40 · PVG 18:40 · LAX 03:40 · JFK 06:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.