I ran into an issue a couple days ago where I started getting the following warning message from Bluebird after using it to promisify mongoose so I could use promises instead of traditional callbacks:Warning: a promise was created in a handler at INSERT-PATH-HERE but was not returned from it, see http://goo.gl/rRqMUwHere is an example of express middleware code that causes this warning:exports.getUser = function(req, res, next) { var userId = req.headers.user_id; User.findById(userId) .then(function(user) { req.user = user; next(); }) .catch(next); }; I completely understand at this point that Bluebird is giving me the warning because a promise is being created in my then handler but not being returned. It turns out that next in Express is the cause of this.I feel fairly comfortable with node but just recently went through the YDKJS books and still don't feel super comfortable with promises so I wanted to make sure I understand what is happening here.Is next basically creating a promise under the hood, and for whatever reason the express team decided not to return it to me? If I assign next(); to a variable it logs as undefined, and obviously if next is commented out then the warning disappears.If I'm correct, I guess I'm looking for some insight into why next doesn't actually give me access to the promise that it's creating? Hoping that by learning this it'll increase my understanding of promises and the design decisions being made here.
Submitted December 03, 2016 at 08:29PM by m9js
No comments:
Post a Comment