javascriptreturnmultilineline-continuation

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


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(...)
    .

Solution

  • 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(...)
    );