1

I've been learning about js for a while, I almost understand about its main concepts such as prototype, oop ... however I can't understand how this works:

User.findOne().where('socialid').equals(id).where('socialnetwork').equals(snw).exec(function (arr,data) {
    if(data){
    res.send(data);
    }
});

What I don't understand is how data and arr variables are passed to the function inside the exec() function. Is it a closure ? Can anybody give a more simple and intuitive example how it execute behind the scene ?

EDITED: actually, I do understand the callback behavior, what I can't understand is data supposed to be a result by User.findOne().... not from inside the callback function of exec() and it only passed the new function to the exec() as I can see in the code.

Thank you so much

6
  • 2
    Why are you all downvoting the question ? We were all new once... Commented Sep 25, 2013 at 17:09
  • It sounds like you don't understand the API you're using, and that you should read the documentation of whatever DSL that is. Commented Sep 25, 2013 at 17:13
  • Thanks Patsy, such a nice comment. Commented Sep 25, 2013 at 17:13
  • Hi Matt, I just wants to understand deeper under the hood, I totally understand the API (express.js in this case) but as I am still new to js so I can't really understand what's going under the hood of that function. Commented Sep 25, 2013 at 17:15
  • Under the hood of which function? Have you read the relevant part of the source code yet? If not, why not? If so, what didn't you understand? Commented Sep 25, 2013 at 17:18

4 Answers 4

4

The source code of exec will look something like this:

function exec(someCallback) {
    // ...
    var arr = /* ? */;
    var data = /* ? */;

    someCallback(arr, data);
    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Matt, thanks for the answer, actually, I do understand the callback behavior, what I can't understand is data supposed to be a result by User.findOne().... not from inside the callback function.
2

Returning a value and calling a callback with the value are actually pretty similar! The fancy name for it is Continuation Passing Style and in fact some programming languages have features (LISP's call/cc, C#'s async, etc) that let you write code in "regular style" but compile it down to continuation-passing-style behind the scenes.

Comments

0

Those are regular parameters.

They are passed when the inner function is called, the same way that any other function is called.

Closures refer to the fact that the inner function can access variables from the outer function (eg, res)

Comments

0

Thank you for all your answers, I understood the concept, and here are the example I want to share for other guys:

  function Person(){};

  Person.prototype = {
    constructor: Person,
    name : "Nicholas",
    age : 29,
    job : "Software Engineer",
    friends : ["Shelby", "Court"],
    sayName : function () {
      console.log(this.name);
    },
    hello : function (callback) {
      console.log('calling back');
      age = this.age; 
      callback(age); //we can use this.age, but for learning purpose, this' better.
    }
  };
  var person1 = new Person();
  person1.hello(function(age){console.log(age + 50);})

So, the age param was identified in the callback.

Cheers

Comments

Your Answer

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.