Closed rjyo closed 11 years ago
I think this can be solved with the help of Promise:
var result = {};
result.user = User.find(req.body.friendId);
result.addFriend = result.user.then(function(friend) {
return me.addFriend(friend);
});
res.json(result);
The query User.find()
should be executed only once according to the spec of Promises/A+: http://promises-aplus.github.io/promises-spec/
Cool!
If all I need is to do run the promises and just send back a 200 response if no error happened. (e.g. add a friend)
What should I do?
res.json(User.find(req.body.friendId));
works well if you don't care about the response body, otherwise:
User.find(req.body.friendId).then(function() {
res.json(/* response body */);
});
Got it. Great lib!
Say sth like the following