Here the function returns an anonymous function:
function respondWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function(entity) {
if(entity) {
return res.status(statusCode).json(entity);
}
return null;
};
}
Why is it that we are returning an anonymous function here that returns a value? What advantages do we have and when to try something like this?
How and what is this anonymous function's argument
entityis populated with? We passed res torespondWithResultand what exactly happens next that we gotentityfor an argument in the anonymous function? What value does this argument get populated with?In case it gets populated with
resonly, what's wrong with directly doing this instead:if(res){ //bla bla } return null
Edit: The function is called like this:
return Outlet.find().exec()
.then(respondWithResult(res))
.catch(handleError(res));
Now, res is what we pass to respondWithResult. Is that what the anonymous functions gets in the argument? If yes, what is the advantage?Why not use res directly?