29

I'm learning ES6 fat arrow functions. What is the correct way to change this code, so as to be able to put another line, even const a = 100; in the place indicated so that I can add some more lines to this function?

IMAdded: (options, args) => ({
    IMAdded: {
        filter: newIM => true, *need to add another line here*
    },
}),

Update:

Here's ES6 code that runs:

const subscriptionManager = new SubscriptionManager({
    schema,
    pubsub,
    setupFunctions: {
        APPTAdded: (options, args) => ({
            APPTAdded: {
                filter: appointment => (true),
            },
        }),
});

I'd like to add some more lines into the code that returns true.

nem035
  • 34,790
  • 6
  • 87
  • 99
VikR
  • 4,818
  • 8
  • 51
  • 96
  • in **IMAdded** you have an object (not a function), you would need to do: **a:100**, then read as IMAdded.a //=> 100 – Hosar Jan 13 '17 at 03:23
  • If you show us the proper ES5 way to declare what you're trying to do, we can easily help you change it to ES6 with fat arrows. With what you show now it is not clear enough what you are trying to achieve for us to know how to fix. You have several combinations of invalid syntax and are declaring objects where we aren't sure you mean to. So, please clarify the objective. The most surefire way to show the objective would be to show working ES5 code for what you're trying to do. – jfriend00 Jan 13 '17 at 03:37
  • Thanks for your advice guys. I have no idea what this would look like in ES5. I'll update the post to show more info. – VikR Jan 13 '17 at 04:23

3 Answers3

54

If you want to convert the following method into having more lines:

{
  filter: appointment => true
}

You have to add curly braces and a return statement:

{
  filter: appointment => {
    // ... add your other lines here
    return true;
  }
}
nem035
  • 34,790
  • 6
  • 87
  • 99
2
filter: appointment => true,
...

is (and parentheses aren't needed around true) a shortcut for

filter: appointment => {
  return true;
},
...

which is a shortcut for

filter: function (appointment) {
  return true;
}.bind(this),
...

Any amount of lines can be added before return statement.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
1

No braces or return needed if your function body is an expression which can span multiple lines (a ConciseBody in the spec)

{
  filter: appointment =>
    true &&
    false
}

Note: I was initially confused about the question (hence the EDIT), and thought it was about linebreaks in arrow functions in general. And, yes, they are allowed, but only after the arrow.
See also the ESLint rule implicit-arrow-linebreak, which lists this variant under "Correct code" (if configured "with the below option")

kubi
  • 829
  • 2
  • 14
  • 24