Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a callback function passed as a parameter in Javascript has arguments or not?

I have to do this

doSomething
    .then(success)
    .catch(failure);

And success and failure are callback functions which will get their value at run time (I am trying to write a module).

So I have to know that the function failure which is sent as a callback should have a parameter.

Because the callback is supposed to be like

function(error) {
// An error happened
}

Is there anyway in JS to do this? Or any library that makes this kind of checking possible?

EDIT

Just to clarify. I want a user(not end user), who uses my module, to send in a function as a parameter which will become a callback and I want to check if the function sent, which will be called back, has enough parameters required. I understand it is up to one who uses it to take care of that. But why not try?

like image 987
Mohammad Aasif Avatar asked Dec 23 '22 07:12

Mohammad Aasif


2 Answers

With a promise callback (your then and catch handlers), they will only be passed a single argument when called, and will always be passed that argument. (If the promise is obeying native semantics; jQuery's Deferred doesn't always.)

In the general case:

Usually you don't care, because in JavaScript, you can call a function with either fewer or more arguments than it has formal parameters. That is, this is just fine:

function foo(a) {
  console.log("a = " + a);
}
foo();        // Fewer arguments than params
foo(1, 2, 3); // More arguments than params

Another reason you don't care is that the function can use arguments even when it doesn't declare formal parameters for them, by using a rest parameter or the arguments pseudo-array (you'll see why this snippet logs foo.length and bar.length in a moment):

function foo() {
  console.log("First argument: " + arguments[0]);
}
function bar(...rest) {
  console.log("First argument: " + rest[0]);
}
foo(1);                                    // First argument: 1
bar("a");                                  // First argument: a
console.log("foo.length = " + foo.length); // foo.length = 0
console.log("bar.length = " + bar.length); // bar.length = 0

So just define what your API will call the callbacks with, and do that, and it's up to the users of the API to ensure that they use what you call them with correctly.

On those occasions it matters, you can use the function's length property to know how many formal parameters it declares:

function foo(a) {
  console.log("a = " + a);
}
console.log(foo.length); // 1

Note that that will tell you how many formal parameters are declared...

  • ...not counting a rest parameter (...identifier)
  • ...only up until the first parameter with a default value (even if there are ones without a default after it)

So for instance:

function foo(a, b = false, c) { // b has a default value
  console.log("a = " + a);
}
console.log(foo.length); // 1, even though `c` has no default value,
                         // because `c` is after `b`

and

function foo(a, ...rest) {
  console.log("a = " + a);
}
console.log(foo.length); // 1, because the rest parameter doesn't count
like image 168
T.J. Crowder Avatar answered Dec 27 '22 21:12

T.J. Crowder


It's probably an anti pattern for the caller to enforce a certain number of arguments of a callback, but if you must you can check the length of the function:

function test() {}

function test1(a) {}

function test2(a, b) {}

console.log(test.length, test1.length, test2.length)
like image 24
Mark Avatar answered Dec 27 '22 19:12

Mark