首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nodejs assert模块: assert.throws只返回undefined

nodejs assert模块: assert.throws只返回undefined
EN

Stack Overflow用户
提问于 2020-04-17 16:52:12
回答 1查看 198关注 0票数 0

official documentation中可以找到。尝试下面的代码只会导致undefined

代码语言:javascript
复制
assert.throws(
  () => {
    throw new Error('Wrong value');
  },
  Error
);

我发现了一些关于箭头函数=>在语句中不起作用的问题,但将其更改为function()并不会改变任何事情。在上面的链接中,我的节点版本是完全相同的版本。

我遗漏了什么?

编辑:

代码语言:javascript
复制
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

非常感谢

EN

回答 1

Stack Overflow用户

发布于 2020-05-04 23:05:13

因此,正如@tkausl在注释中所说的(顺便说一句,谢谢。) assert.throws“断言传递的函数抛出”。

为了达到我想要的结果,我不得不这样做:

代码语言:javascript
复制
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
}())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61267617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档