【发布时间】:2018-04-12 06:30:29
【问题描述】:
我似乎有一个未处理的承诺拒绝,我找不到...
我已经使用--trace-warnings 运行 nodejs,并且我的脚本顶部有以下代码:
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at:', p, 'reason:', reason);
console.log(p)
console.log(reason)
});
当 promise 被拒绝时,我看到了:
Unhandled Rejection at: Promise { <rejected> null } reason: null
Promise { <rejected> null }
null
我检查了我拥有的几个异步函数和承诺,它们都有.catch() 的...这里到底发生了什么?我怎样才能知道拒绝的来源?
一些代码。我的班级中有以下异步函数:
async loop() {
console.log("starting loop")
if ( this.state != this.STOPPED ) {
switch (this.case) {
case this.POLLING:
console.log("Start polling")
await Promise.all(this.symbols.map(symbol => {
return this.fetch(symbol)
})).then(() => {
...
}).catch(error => {
...
})
for (let symbol of this.symbols) {
await this.process(symbol).then(() => {
...
}).catch(error => {
...
})
} //for
for (let openPosition of this.openPositions) {
this.log.push(openPosition.toLogString())
}
console.log("end polling")
break
} //state selection case
console.log(this.log.formatTimestamp(Date.now()))
this.log.dump()
this.log.clear()
console.log("done dumping")
setTimeout(this.loop.bind(this), 0)
}
}
奇怪的是输出,它重复:
starting loop
Start polling
end polling
done dumping
Unhandled Rejection at: Promise { <rejected> null } reason: null
Promise { <rejected> null }
null
警告基本上显示在
setTimeout() 打电话。不知道为什么这是一个问题。另外值得注意的是,根据数组this.OpenPositions 中有多少元素,我似乎得到了不同数量的承诺拒绝。对于 n>1,我得到 n-1 个错误,其中 n 是元素的数量。
【问题讨论】:
-
你可能写了一个你可能没有正确处理错误响应的承诺。你能添加一些代码吗?
-
您能详细说明一下吗?我不能只发布每个异步函数或承诺,但如果有特定的东西我可以发布块。
-
控制台或终端中必须有一个行号指示错误
-
嗯,你需要缩小哪个承诺有未处理的拒绝。我希望当您显示此错误并将其缩小到那些时,您可以弄清楚操作中涉及哪些承诺。您也可以暂时或永久切换到Bluebird promise library,这更有可能为您提供未处理拒绝的堆栈跟踪。
-
任何地方都没有行号,因此是我的问题。如果我去掉
process.on(),它会给我一个类似的消息,没有行号。 nodejs 通常会给我一个未处理的承诺拒绝的跟踪,所以我不确定这里发生了什么。弹出此警告时正在执行的 case 语句有一个Promise.all(),它有一个.catch(),还有一个await function(),它也有一个。
标签: javascript node.js