How to find out parameters and theirs order from various callback functions?


#1

Hello

Could somebody tell me how to find out what’s the right order of callback parameters for some functions?

I’ll provide an example.

For submitting form there is example piece of code:

Template.body.events({
    "submit .new-task": function (event) {
      // Prevent default browser form submit
      event.preventDefault();
 
      // Get value from form element
      var text = event.target.text.value;

As you can see callback function has one parameter called “event”. I’ve seen also two parameters where second one was for template etc.

My question is: Why is every-time “event” first and then “template” and so on? How can i figure out what’s correct order for this callback function and that first parameter will return event, second template and if there is some trhird parameter???

Another example:

if (Meteor.isServer) {
    exec = Npm.require('child_process').exec;
    Fiber = Npm.require('fibers');

    _execSync = function(cmd, stdoutHandler, stderrHandler) {
        exec(cmd, Meteor.bindEnvironment(
                function(error, stdout, stderr) {
                    if (stdout != "")
                        stdoutHandler(stdout);
                    if (stderr != "")
                        stderrHandler(stderr);
                }
            )
        );
    }

Callback for function “execSync” has 3 parameters. How do I know that first belongs to “cmd” or “error” or something else and second is stdout and not stderr and so on?

Thank you


#2

You need to check the API for the resources to find out what arguments the fnction takes and and what order

for the event handler section in the docs says

The handler function receives two arguments: event, an object with information about the event, and template, a template instance for the template where the handler is defined

it could be clearer, especially the examples which only use the event argument

event is first because that’s the way it was developed, when the callback is invoked, it will given two arguments representing the event and the template in that order.

Same for all call backs.

You’ll often see anonymous callbacks written like this function(err, res){} but it’s a bit of fudge because only one of the arguments will be instantiated, the other will undefined. They both need to be present because when they are coded the outcome is indeterminate, This is why we test in the function to see if the err argument is undefined before decided on the procedure of the code.

Hope this helps, its not a meteor thing either, its javascript. There are plenty of further resources for you on the internet.


#3

YES!

That’s exactly what I’ve been looking for.

So now I know why “exec” callback is in order “err, stdout, stderr”… because of implementation.

callback Function called with the output when process terminates
error Error
stdout Buffer
stderr Buffer

Once again thank you.