0
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?

asked Nov 2, 2012 at 20:33
1
  • 1
    One could argue that all JS functions are closures, as the parent scope is there automatically (with no extra work on your part) if you want variables from it. Some just don't use those variables (yet). Commented Nov 2, 2012 at 21:01

3 Answers 3

1

The others have already answered most of your questions. However, from your comments I see that you may still be confused about the last question:

  • Q: will the above function work if callback wasn't defined anywhere?

Answer: callback is already defined in the above code. And here's where callback is defined:

exports.list = function(callback){ // <---- the callback variable defined here
 var result = model.find({}, function(err, objects){
 callback(null, objects) // <-- callback is used here
 });
 return result;
}

Now, by the strict meaning of the word "defined", callback is defined as the parameter of eports.list(). If you pass anything other than a function (or nothing) the exports.list() callback would still technically be "defined" but its value would not be a function.

answered Nov 2, 2012 at 21:32
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot, that REALLY cleared up my confusion because i come from a Java background where a function with a parameter must be defined. cheers!
0

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 call callee (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

answered Nov 2, 2012 at 21:18

1 Comment

referring to @slebetman's answer, will i still get a ReferenceError: callback is not defined if i call exports.list(), where callback is not defined elsewhere
0

it is a closure because callback is captured inside the model.find() code from enclosing function scope assigned to exports.list.

answered Nov 2, 2012 at 20:36

5 Comments

yup i got the captured bit. but its now a parameter of the function, instead of examples of callback being called by other functions outside the scope of list
this example just expects you to call the list function bound to exports and pass it the callback. something like exports.list(function(arg1, objList) { console.log("stuff"); })
i see. in other words, i have to define callback right? As i dont see it being defined as a function elsewhere..
yes, you need to provide the callback. the exports.list function would just use whatever callback you pass it to do some setting up or whatever first before calling the custom code you give it.
@bouncingHippo: callback is already defined in your code. Here: exports.list = function(callback){ // <-- callback defined here

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.