跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 为什么这个 try-catch-finally 没有没有执行 finally 部分?
未分類
3 4 月 2021

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

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

資深大佬 : xialvjun 0

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');   } })(); 

直接输出

[email protected]:~/c/test_got ➤ ts-node index.ts try [email protected]:~/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); 

输出

[email protected]:~/c/test_got ➤ ts-node index.ts try waiting   

大佬有話說 (15)

  • 主 資深大佬 : xialvjun

    已经 59 次点击了,为什么都没人回复呢?难道这问题太简单了?我是有什么基础点没学到位吗?(怀疑人生中)。
    好奇这问题,如果也是不知道为什么的,也回复一下啊。至少让我知道不是我一个人菜啊。

  • 資深大佬 : Vegetable

    @xialvjun
    是因为复现不了你的问题。

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

    都能正常打印 finally

    给的输出,为什么没打印出数据或者 error ?

  • 主 資深大佬 : xialvjun

    奇怪,我用 pnpm 安装的。。。
    我换换 npm 看下

  • 資深大佬 : fy136649111

    14.5.0 的 node 和 8.10.2 的 ts-node 复现不出来

  • 資深大佬 : faceRollingKB

    你没发现你的输出既没有 console.log(‘got’, res);又没有 console.log(‘error’, error);么?这说明 await 的部分还在等待啊笨

  • 資深大佬 : libook

    “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

    我上传下 git 仓库吧。。。
    ➤ node –version
    v14.10.0
    [email protected]:~/c/test_got
    ➤ ts-node –version
    v9.1.1

  • 資深大佬 : cuzfinal

    控制台不能输出异步结果,在浏览器里试试

  • 主 資深大佬 : xialvjun

    https://github.com/xialvjun/node_bug_maybe

  • 資深大佬 : zhw2590582

    实测,没问题

  • 主 資深大佬 : xialvjun

    @libook 改成 require 语法,用 node 执行也不行。都是只输出个 “try” 就退出了。。。 而换成别的 异步函数,例如 `const delay = () => new Promise(re => setTimeout(res, 1000))` 就完全正常。想着会不会是 got 的 bug,但是 `grep -RIn process.exit ./` 都没发现有哪个包写了 process.exit

  • 主 資深大佬 : xialvjun

    郁闷,我都要感觉我是不是应该录屏来自证清白了。

  • 資深大佬 : libook

    @xialvjun 你 Node 啥版本,我是 v15.13.0,你换个版本试试?

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

    不用改 require 语法,node 是支持 ESM 的,只需要在 package.json 加上”type”: “module”就可以了,当然前提是你当前的 node 版本比较新,不需要 flag 就可以直接用 ESM 。

  • 主 資深大佬 : xialvjun

    https://github.com/sindresorhus/got/issues/1441
    https://github.com/nodejs/node/issues/35116
    艹,真巧,让我碰上了。

  • 資深大佬 : libook

    个人的经验来看,LTS 最好时刻保持最新,因为 LTS 的更新都是增强可靠性和安全性的更新。

    不过我在公司核心项目上一直在用 Current 版本,也是时长会更新,品控做得还不错,很多年来没遇到什么问题。

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具