最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript: clean way to have a return statement span multiple lines - Stack Overflow

programmeradmin1浏览0评论

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 e up with is the hackish

return false ||
    someValue;

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

return false ||
    myThing.
    doSomething()
    .then(...)
    .then(...)
    .

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 e up with is the hackish

return false ||
    someValue;

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

return false ||
    myThing.
    doSomething()
    .then(...)
    .then(...)
    .
Share Improve this question edited May 9, 2019 at 18:48 orion elenzil asked May 9, 2019 at 18:42 orion elenzilorion elenzil 5,4934 gold badges45 silver badges56 bronze badges 5
  • 5 I prefer parentheses: return ( <newline> someValue <newline> ); – p.s.w.g Commented May 9, 2019 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 Commented May 9, 2019 at 18:44
  • i would move the or below the return statement. the operands have the same column. – Nina Scholz Commented May 9, 2019 at 18:45
  • If it's a number you can use return + <newline> someValue; – Barmar Commented May 9, 2019 at 18:47
  • A mon idiom is return myThing <newline>.doSomething() <newline>.then() <newline>.then() <newline>.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 Commented May 9, 2019 at 18:52
Add a ment  | 

1 Answer 1

Reset to default 6

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(...)
);
发布评论

评论列表(0)

  1. 暂无评论