为什么这个 try-catch-finally 中的 finally 部分没有执行,Nodejs 环境下的问题?

为什么这个 try-catch-finally 中的 finally 部分没有执行,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’);
}
})();

直接输出

didi[@localhost](/user/localhost):~/c/test_got
➤ ts-node index.ts
try
didi[@localhost](/user/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](/user/localhost):~/c/test_got
➤ ts-node index.ts
try
waiting


回到顶部