我有以下Javascript代码
main.js
'use-strict';
const assert = require('assert');
var r = require('./index.js');
async function fun() {
try {
r.start();
} catch (e) {console.log(e);}
}
fun();index.js
'use-strict';
var assert = require('assert');
async function start() {
assert.ok(1==0);
}
exports.start = start;现在main.js中没有捕捉到index.js中抛出的异常,为什么呢?我有什么方法可以促进这一点吗?
我在不同的文件中有一系列从main.js调用的函数,我不想把try/catch放在所有的文件中。
发布于 2021-01-08 22:05:34
如果您将main.js更改为此值,则有望捕捉到错误,因为该函数将等待承诺的解决,并在main.js中抛出错误。
main.js
'use-strict'
const assert = require('assert')
const r = require('./index.js')
const fun = async () => {
try {
// Wait for the promise to resolve
await r.start()
} catch (error) {
console.log(error)
throw new Error(`[main.js] - Error was thrown ${error}`)
}
}
fun()https://stackoverflow.com/questions/65630147
复制相似问题