我有一个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;
但现在我问自己,是不是真的等待回调结束?这是处理这种警告的正确方法吗?我有感觉我做错了。
我记得在编写快件中间件时遇到同样的问题,然后在承诺中调用next()
函数跳转到下一个中间件。它也会返回undefined
。任何建议来处理这个?
尝试从回调函数'cb(null,true)'返回'null'。此警告显示创建失控承诺。 [详情](http://bluebirdjs.com/docs/warning-explanations.html#warning-a-promise-was-created-in-a-handler-but-none-were-returned-from-it) – hassansin
感谢回复,但我无法控制cb返回的内容,因为它是一个外部库。例如express中的'next()'方法。 – kgalli