1. The Promise.all() method *returns a single Promise that resolves when all of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with the reason of the first promise that rejects.[1]*
so it will return a promise when User.findById and execPopulate promise is resolve 2. From the look of it, you shouldn't need to repopulate the author, take it out see if still works as inteneded :-) [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all On Wednesday, 6 September 2017 15:15:19 UTC+1, Rohan Pota wrote: > > The tutorial that I am follwin has the following 2 router methods for > getting the articles from db. > > router.param('article', function(req, res, next, slug) { > Article.findOne({ slug: slug}) > .populate('author') > .then(function (article) { > if (!article) { return res.sendStatus(404); } > > req.article = article; > > return next(); > }).catch(next); > }); > > router.get('/:article', auth.optional, function(req, res, next) { > Promise.all([ > req.payload ? User.findById(req.payload.id) : null, > req.article.populate('author').execPopulate() > ]).then(function(results){ > var user = results[0]; > > return res.json({article: req.article.toJSONFor(user)}); > }).catch(next); > }); > > I have two questions regarding the methods above, > > 1. How does `Promise.all()` work ? > 2. Why do we need to repopulate author field in `router.get()` method > when already did that in the `router.param()` method? > > -- Job board: http://jobs.nodejs.org/ New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/74542f61-8cc5-446c-b2bf-1a93c28ad8b4%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
