2

I saw the following line in a node js code base.

_ => { return resolve(abc);}

Can any one give me some hint on what this _ means? I know => is fat arrow function. This line is from a snippet of promise.

ZZZ
  • 3,574
  • 10
  • 34
  • 37

4 Answers4

5

At the coaxing of a discussion elsewhere in this question, I'll turn my comments into an answer.

First off, I assume you aren't asking about arrow functions in general, but are just curious about the _ in your code sample.

There is a convention (used by some) to declare and name a parameter to a callback function that you know is passed, but you are not going to be using with an underscore as a sort of placeholder. It's a signal to people reading the code that there is an argument being passed to this callback, but you don't plan on using it in this particular invocation. It's presence is not functional, but more for

Now, without knowing what was in the head of the developer who wrote the line of code you asked about, we can't know for sure what the underscore means, but it does appear to match that previously described convention. There is one argument to the callback and it is not used in the implementation of the callback.

So, if our guess that this is a use of that convention is true, then in the arrow function that you show:

_ => { return resolve(abc);}

It is likely expecting one argument, but this particular use of that callback does not plan on using it, thus they named it with an _ just to signal that.


Now, there is no particular reason in Javascript that the callback needs to even define a single argument like this that be used. The callback could just as well have been defined as:

() => { return resolve(abc);}

since there's no requirement in Javascript that you declare arguments you aren't going to use.

If the callback took two arguments and it was the first one that you weren't going to use, then it does have to be declared as something in order to allow access to the second argument:

(_, secondArg) => { return resolve(secondArg);}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Excellent response. I'm not an English speaker so i couldn't present this through response without getting into trouble :-). Thanks @jfriend00. I try to downvote my own answer but SO don't let me. – yeiniel Jul 12 '17 at 02:17
  • 1
    @yeiniel - If this answered your question, then you can indicate that to the community by clicking the checkmark to the left of the answer. That will also earn you some reputation points for following the proper procedure. – jfriend00 Jul 21 '17 at 21:32
4

It means that the arrow function argument is not used. They use _ as argument name for the arrow function.

yeiniel
  • 2,416
  • 15
  • 31
  • Using `_` as an argument is an oft-used convention as a placeholder for an unused argument that you know is passed, but you don't intend to use. Since `_` is a perfectly legal symbol name in Javascript and we don't have access to the author's thinking here, it's just a guess that the author was using that convention (though it is probably the case). I think you should modify your answer to say that this is "probably" the case since you have no proof - it's not an actual language feature. – jfriend00 Jul 11 '17 at 21:42
  • @jfriend00 you are absolutely right. And it would be more fare if you publish your own answer. The right answer is not selected yet. – yeiniel Jul 12 '17 at 00:15
  • I was offering you an opportunity to "improve" your answer to make it the best answer. You can use the "edit" link to incorporate feedback and improve your answer. The goal here is only to make your answer the best it can be. – jfriend00 Jul 12 '17 at 00:25
  • @jfriend00 i want to thank you for your comment. But i think that the right answer is the one in your comment. When i read your comment i realized that my answer is not near close to the right one. That's why i propose you to write your own answer because i think is not fair for me to take your answer as mine own. – yeiniel Jul 12 '17 at 00:45
  • OK, I wrote an answer. I was offering to let you take the improvements and incorporate it yourself. but at your coaxing I wrote an answer. – jfriend00 Jul 12 '17 at 01:28
1

Doesn't this mean something like this?

function(_){
    return resolve(abc);
}
Iruss
  • 229
  • 4
  • 13
1

What's happening here is that the code is using (some of) the concise syntax for arrow functions. To do this you'd write a function like this:

var f = x => x*x;

according to the docs

So with your example, the argument x is named with an _ (a valid character for variable names):

var f = _ => {return resolve(abc)}

But, it doesn't need the return or the brackets, either. It could be just:

_ => resolve(abc);

Perhaps a more readable way to write it would be with the parathesis:

() => resolve(abc);
Cruiser
  • 1,618
  • 2
  • 16
  • 20