【发布时间】:2016-08-14 06:54:47
【问题描述】:
我有一个 nodejs express 应用程序,我使用了一个库,它有一个典型的回调接口来执行函数。我的持久层使用基于 Promise 的方法。我有以下代码困扰我
getUserByName('dave')
.then(function (user) {
// check stuff and call the callback with success
return cb(null, true);
})
.catch((err) => cb(err, false));
问题:cb(null, true) 函数返回 undefined 并且承诺以警告 a promise was created in a handler but was not returned from it 结束。
我可以通过运行回调来解决这个问题,然后像这样执行return null:
// check stuff and call the callback with success
cb(null, true);
return null;
但现在我问自己真的是在等待回调完成吗?这是处理这种警告的正确方法吗?我觉得我做错了。
我记得在编写 express 中间件时遇到同样的问题,然后在调用 next() 函数的 promise 中跳转到下一个中间件。它还返回undefined。有什么建议可以解决这个问题吗?
【问题讨论】:
-
尝试从回调
cb(null, true)返回null。此警告显示创建失控的承诺。 more info -
感谢您的回复,但我无法控制 cb 返回的内容,因为它是一个外部库。例如 express 中的
next()方法。
标签: javascript node.js express promise sequelize.js