如在《公约》中发现 公文. 尝试下面的代码只会导致 undefined
.
assert.throws(
() => {
throw new Error('Wrong value');
},
Error
);
我能够找到一些关于箭头函数的问题。=>
语句中没有工作,但将其改为 function()
并没有改变什么。就像上面的链接中,我的节点版本是完全一样的版本。
我错过了什么?
EDIT:
console.log(assert.throws(
() => {
throw new Error('Wrong value');
},
Error
)) // this will log "undefined"
let result = assert.throws(
() => {
throw new Error('Wrong value');
},
Error
);
console.log(result); // as well as this
非常感谢
所以,作为 @tkausl 在评论中说过(顺便说一句,谢谢。assert.throws
"断言传递的函数会抛出"。
为了达到我想要的结果,我不得不这样做。
console.log(() => {
let error;
try {
// function that may throw an error
} catch (e) {
error = e;
}
// and than do something with 'error' and return a value
return error
}())