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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With