Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skipping parameters in callback function

Tags:

The callback function I'm working with has the following signature (from http://api.jquery.com/load/):

complete(responseText, textStatus, XMLHttpRequest)

Now, I only need the third parameter. In Lua there's a convention where an underscore is used to skip unneeded return values from functions (skip because _ will actually hold the value):

var1, _, _, var4 = func()

So I thought of doing a similar thing with JavaScript and set my function signature to this:

function (_, _, XMLHttpRequest)

Is there anything wrong with this approach, perhaps there's a better/cleaner way?

like image 255
user247702 Avatar asked Mar 27 '12 11:03

user247702


People also ask

Can we pass arguments in callback function?

A callback function is a function that is passed as an argument to another function, to be “called back” at a later time. A function that accepts other functions as arguments is called a higher-order function, which contains the logic for when the callback function gets executed.

How do I make a callback optional?

You can also make an argument optional by giving it a default value in the declaration: function save (callback=noop) { ... If the User provides something that is not a function, then you can allow it to crash as it is a violation of the contract. No need to be handling every possible scenario.

What is the problem with callbacks?

Error handling in nested callbacks As you can see, we have to handle error in every single callback and it's actually a repetition of a boilerplate to handle errors. So this code is not DRY (we as programmers don't like to repeat the same code over and over again) and it's kind of difficult to reuse an error handler.

Can callback be asynchronous?

The main difference between synchronous and asynchronous callbacks is that synchronous callbacks are executed immediately, whereas the execution of asynchronous callbacks is deferred to a later point in time.


2 Answers

I acknowledge that using _ is a common pattern to omit parameters that prepend the one you want. That's cool for one parameter, maybe 2.

somethingWithACallback((_, whatIAmLookingFor) => { 
  // ...
})

but I got stuck needing the 5th one. This would mead I'd have to write

somethingWithACallback((_, __, ___, ____, whatIAmLookingFor) => { 
  // ...
})

For that case I propose this pattern:

somethingWithACallback((...args) => { 
  const whatIAmLookingFor = args[4];
})

With destructuring, you can also do this

somethingWithACallback((...args) => { 
  const [,,,,whatIAmLookingFor] = args;
})

and apply that to multiple parameters

somethingWithACallback((...args) => { 
  const [,,,,whatIAmLookingFor,,andAnotherThing] = args;
})

and thereby essentially pick what you need.

like image 45
Lukas Avatar answered Sep 21 '22 00:09

Lukas


The technique is not pretty, but I use it myself on several occasions. I guess it is still quite better to give those unused arguments meaningful names (just to avoid confusion), but you're fine in using underscores.

I often see it used in jQuery related callbacks, where the index is often passed in as first argument, like

$('.foo').each(function(_, node) {
});

because most of the time, you don't care about the index there. So to answer your actual question, there is nothing wrong in using the technique (beside confusion maybe) and there is no better/cleaner way to skip unwanted arguments.

like image 175
jAndy Avatar answered Sep 19 '22 00:09

jAndy