exports.list = function(callback){
var result = model.find({}, function(err, objects){
callback(null, objects)
});
return result;
}
Q: Why is the above code a closure? Is it because the function parameter calls the function callback which is nested in it?
Q: does this cause an infinite loop, since the function is calling itself through its parameter?
Q: will the above function work if callback wasn’t defined anywhere?
Q: Why is the above code a closure? Is it because the function parameter calls the function callback which is nested in it?
A: As Mike explained, once an outside var is used inside a function, you create a closure.
Q: does this cause an infinite loop
A: I assume
function(err, objects)will fire only once for the model, so no it won’t. You would create an infinite loop if you callcallee(or self)Q: will the above function work if callback wasn’t defined anywhere?
A: you will get an error
ReferenceError: callback is not defined