DEV Community

Discussion on: ES6, the arrow function

Collapse
 
washingtonsteven profile image
Steven Washington • Edited

I love using arrow functions as anonymous callbacks passed into functions (or promises)! Typically, the scope magic as a part of arrow functions is just what I want in that context.

That said, I try to continue to use braces (even though they are optional for single statements, same for if statements), and also explicitly use the return keyword. That helps with my two big hangups on reading arrow functions.

// ehh...okay
let square = (x) => x*x; 

// me likey
let squareBetter = (x) => { return x*x; }

// now we're talking!
// I mean, I don't know why you'd need to asynchronously get a number...
// but here it is:
let xSquared = -1;
asyncGetANumber().then((x) => {
  xSquared = x*x;
}));

In almost any other situation (i.e. functions defined in a class, or as part of another object, or gasp globally), I use standard function syntax.