3

i'd like to have a javascript return keyword on one line, and the thing it actually returns on the next line. in python this would be something like

return \
    someValue

however due to the optionality of statement-terminating semicolons and the absence of a line-continuation character in javascript, the only approach i've come up with is the hackish

return false ||
    someValue;

motivation: my actual code is like this. during development i sometimes comment out the second line to switch between GlobalScope.doSomething() and myThing.doSomething(). Admittedly this is a minor issue.

return false ||
    myThing.
    doSomething()
    .then(...)
    .then(...)
    .
orion elenzil
  • 4,484
  • 3
  • 37
  • 49
  • 5
    I prefer parentheses: `return ( someValue );` – p.s.w.g May 09 '19 at 18:44
  • indeed. that's better for a simple case! but in my actual actual code, the doSomething() is a promise chain which goes on for 10 or 15 lines and i'd prefer not to have a somewhat spurious close-parens at the end of them. – orion elenzil May 09 '19 at 18:44
  • i would move the or below the return statement. the operands have the same column. – Nina Scholz May 09 '19 at 18:45
  • If it's a number you can use `return + someValue;` – Barmar May 09 '19 at 18:47
  • A common idiom is `return myThing .doSomething() .then() .then() .then()` i.e. the `return myThing` on one line acts as an anchor for the rest of the fluent chain. Also, "*I'd prefer not to have a somewhat spurious close-parens*" Yeah, I agree, JS is full of trailing syntax like that IMHO. You kind of just get used to it at some point. – p.s.w.g May 09 '19 at 18:52

1 Answers1

5

The usual approach is to put the object from which you are chaining in the same line as the return:

return myThing
    .doSomething()
    .then(...)
    .then(...);

or

return myThing
.doSomething()
.then(...)
.then(...);

but you can also use parenthesis if you insist on separate lines and extra indentation:

return (
    myThing
    .doSomething()
    .then(...)
    .then(...)
);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • thanks Bergi. i'm looking for something where i can comment/uncomment exactly one entire line to change the functionality in the way i want. your third suggestion achieves that, but it introduces a trailing close-paren. for now i'm sticking with the `false ||` thing. – orion elenzil May 09 '19 at 20:51
  • 1
    @orionelenzil You can just swap two `return myThing` and `return otherThing` lines with comments? – Bergi May 09 '19 at 20:53
  • :) yes, i could. – orion elenzil May 10 '19 at 15:57