简体   繁体   中英

Single-Line to Multi-Line ES6 Fat Arrow Function?

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 .

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;
  }
}
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.

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM