Answer the question
In order to leave comments, you need to log in
Why wrap a named function in an anonymous one in JS?
Now I am reading a wonderful book about the stack mean and came across a trick that is strange for me:
function addReview(req, res) {
getLocationInfo(req,res,function(req,res,responseData){
renderReviewForm(req,res,responseData);
});
}
function addReview(req, res) {
getLocationInfo(req,res,renderReviewForm);
}
Answer the question
In order to leave comments, you need to log in
most likely your getLocationInfo function at the time of its execution does some calculations you need, which are then called through a callback call, the only thing that is not clear to me is why they need to pass req res and so they will be in a closure.
For example, let's say your function should work with the location info that is in res.body.locateionData and when the data is received, execute the callback function. where you can process this data. I'll write an example the same way, passing req res so you can get the gist of what's going on.
function getLocationInfo(req, res, cb) {
var responseData = "";
if(res.body.locateionData) responseData = res.body.locateionData;
cb(req,res,responseData);
}
getLocationInfo(req,res,function(req,res,responseData){
renderReviewForm(req,res,responseData);
});
var cb = function(req,res,responseData){
renderReviewForm(req,res,responseData);
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question