Strange callback passed to function


#1

I work at tutorial from main site. Take a look at code snippet below:

if (Meteor.isServer) {
// This code only runs on the server
Meteor.publish(‘tasks’, function tasksPublication() {
return Tasks.find();
});
}

‘Meteor.publish()’ tooks at second parameter callback function. But is not anonymous function and not variable with reference to specific function.
It looks like function definition. Is it permitted to pass function definition as callback function?


#2

This is what’s called a “named function expression”. So while an anonymous function expression looks like:

doSomething(function () {
  console.log('Awesome function!');
});

a named function expression looks very similar but has a name:

doSomething(function myNamedFunction() {
  console.log('Awesome function!');
});

When run both of these functions will behave in a similar fashion, and produce the same output, but named function expressions have a few advantages when it comes to things like debugging. The name of the function will be included in exception stack traces for example (instead of just seeing something like “anonymous function”).

For more info check out the Anonymous vs. Named section of the You Don’t Know JS: Scope & Closures book.