从蓝鸟承诺的.then
函数引发异常时,将显示以下警告消息:Warning: a promise was rejected with a non-error: [object Error]
。
使用以下命令运行测试时会发生这种情况:
这是一个小的代码示例,它使用诺言的实现和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)。
有人可以解释这是否是预期的吗?为什么?