蓝鸟-警告:承诺以非错误被拒绝:[对象错误]

时间:2018-11-07 16:52:13

标签: javascript node.js promise bluebird

从蓝鸟承诺的.then函数引发异常时,将显示以下警告消息:Warning: a promise was rejected with a non-error: [object Error]

使用以下命令运行测试时会发生这种情况:

  • 节点9.6.1
  • bluebird 3.5.3
  • 业力2.0.0
  • 柴4.2.0
  • chai-promise 7.1.1

这是一个小的代码示例,它使用诺言的实现和bluebird的声明,在诺言的解析之后抛出一个Error实例:

import chai from 'chai'
import bluebird from 'bluebird'
import promised from 'chai-as-promised'
const { expect } = chai.use(promised)

describe('warning when promise throws', () => {
  const dummyErrorMessage = 'dummy error message'

  async function throws (p) {
    await p.resolve().then(() => {
      throw new Error(dummyErrorMessage)
    })
  }

  describe('throws', () => {
    it('with vanilla promise', async () => {
      await expect(throws(Promise))
        .to.be.rejectedWith(Error, dummyErrorMessage)
    })

    it('with bluebird promise', async () => {
      await expect(throws(bluebird))
        .to.be.rejectedWith(Error, dummyErrorMessage)
    })
  })
})

哪个输出:

  warning when promise throws
    throws
      ✓ with vanilla promise
WARN LOG: 'Warning: a promise was rejected with a non-error: [object Error]'
      ✓ with bluebird promise

bluebird文档提供了有关该警告的潜在解释,但似乎不合适(http://bluebirdjs.com/docs/warning-explanations.html#warning-a-promise-was-rejected-with-a-non-error)。

有人可以解释这是否是预期的吗?为什么?

0 个答案:

没有答案