1

I am able to run a code like this

myObject.save(function(error, data){
    if(error){
      console.log(error);
    }
    else {
      console.log(data);
    }
});

What i don't understand is where the anonymous function get the arguments error and data from?

2

1 Answer 1

4

Its from the thing that calls it - so save might look like this:

    function save(callback) {
        //do stuff

        var error = false;
        var data = {
            something: 'horse'
        };

        callback(error, data)
    }

There's no magic going on - its just a function call

Sign up to request clarification or add additional context in comments.

1 Comment

I guess the only thing to add here is that in javascript functions are first class citizens so you can pass them around just like you can other objects/ints etc.

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.