Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upRequire a newline after each call in a method chain (newline-per-chained-call) #720
Comments
dcousens
added
the
feature request
label
Dec 13, 2016
This comment has been minimized.
This comment has been minimized.
|
@HR is there an eslint rule for this? |
This comment has been minimized.
This comment has been minimized.
|
@dcousens I checked and couldn't find one. So, I guess not. |
This comment has been minimized.
This comment has been minimized.
|
Probably better off making your case at eslint first, then coming back here |
This comment has been minimized.
This comment has been minimized.
|
@dcousens I think that this rules goes perfectly well with the idea of writing "code that you find beautiful" which is what standard is about from what I understand (not that sure about eslint). I think you would agree with me that code with this rule is much more readable. |
This comment has been minimized.
This comment has been minimized.
|
@HR I don't use promises [yet™] I do use chains though, and I don't use the fancy syntax you promote. |
This comment has been minimized.
This comment has been minimized.
|
@dcousens Ok. I'll try making a case at eslint but I believe that this issue should get more exposure and the input of more devs before an informed decision can be made. |
This comment has been minimized.
This comment has been minimized.
grantcarthew
commented
Dec 14, 2016
|
I don't see that this should be a rule. It's more of a choice for the dev. I use the first example above. I don't see the proposed rule to be that much better. |
This comment has been minimized.
This comment has been minimized.
|
@grantcarthew then how is a "Space after keywords" or a "Space after function name" that much better? The proposed rule makes the code more readable than adding a space after keywords does. Wouldn't you agree? |
This comment has been minimized.
This comment has been minimized.
grantcarthew
commented
Dec 14, 2016
|
I just tried it out on my RethinkDB queries. It seems better. Still think it is a choice though. |
This comment has been minimized.
This comment has been minimized.
|
@grantcarthew so is adding a semicolon! It helps create "code that you find beautiful" because it is cleaner which is why you exclude the semicolon. |
This comment has been minimized.
This comment has been minimized.
grantcarthew
commented
Dec 14, 2016
|
I see your point @HR. I'm no expert though. If you read any tutorial online about Promises they are using the first example syntax above. |
This comment has been minimized.
This comment has been minimized.
|
@HR probably more importantly, is that there is "less of a choice"... again, find an eslint rule, and if its too ambiguous either way (aka, 50/50 for standard users), then I can't see it being enforced. |
This comment has been minimized.
This comment has been minimized.
MrZhangFengfeng
commented
Dec 23, 2016
|
I like yours @HR |
This comment has been minimized.
This comment has been minimized.
|
There's no eslint rule for this specific of a scenario, even in
Thoughts? |
feross
added
enhancement
and removed
feature request
labels
Dec 27, 2016
This comment has been minimized.
This comment has been minimized.
|
@feross I'd see what the damage is first |
This comment has been minimized.
This comment has been minimized.
|
@feross That looks good |
This comment has been minimized.
This comment has been minimized.
|
Since this rule is still being discussed, I’d like to question the indentation of promise chains. Especially for larger chains, it can lead you into thinking that you forgot to close a code block: function doSomething () {
return getURL(URL)
.then((res) => {
return processPromise(res)
})
.then((res) => {
return processMorePromise(res)
})
.catch((err) => {
console.error(err)
})
}This has also been discussed in beautify-web/js-beautify#482, where @brandondrew put it quite well:
Anyway, this probably won’t pass for the majority of standard users, so I can understand why this might not be a good idea. For what it’s worth, I’d really like to see such a rule – regardless of which syntax ends up being |
This comment has been minimized.
This comment has been minimized.
|
Agreed with @sonicdoe that that indentation has thrown me off when reading others code. array
.filter
.map
.reduceBut, I honestly don't have a solid opinion on either. |
This comment has been minimized.
This comment has been minimized.
|
I usually indent anytime an expression extends onto multiple lines, e.g. if (condition1 && condition2 && condition3 &&
condition4 && condition5) { // <-- indented here
console.log('test')
}The indention signals intent: the indented line can't be understood without looking at the previous line. That said, I do appreciate the point you've made. Arguably the dots at the beginning of each method can signal the same thing. And yet, this looks like a bug to me: arr
.map()
.filter()
console.log('test') // is this part of the previous chain?Whereas, this does not: arr
.map()
.filter()
console.log('test') // this is clearer |
feross
added this to the
standard v9 milestone
Jan 11, 2017
feross
changed the title
New rule: break promise chains
Require a newline after each call in a method chain (newline-per-chained-call)
Jan 11, 2017
This comment has been minimized.
This comment has been minimized.
|
@feross I don't mind either, I think your logic:
Is sound, and is readable. |
feross
modified the milestones:
standard v9,
standard v10
Feb 9, 2017
feross
modified the milestones:
standard v11,
standard v10
Mar 2, 2017
This comment has been minimized.
This comment has been minimized.
carmelocatalfamo
commented
May 2, 2018
•
|
Hi, indentation rules can be customizable (or can be disabled)? From getURL(URL)
.then((res) => {
return processPromise(res)
})
.then((res) => {
return processMorePromise(res)
})
.catch((err) => {
console.error(err)
})To getURL(URL)
.then((res) => {
return processPromise(res)
})
.then((res) => {
return processMorePromise(res)
})
.catch((err) => {
console.error(err)
}) |
This comment has been minimized.
This comment has been minimized.
|
@carmelocatalfamo Sorry, there's no way to customize |
feross
modified the milestones:
standard v12,
standard v13
Aug 28, 2018
feross
modified the milestones:
standard v13,
standard v14
Jul 5, 2019
This comment has been minimized.
This comment has been minimized.
|
After further consideration, I don't think the rule to require a newline after each call in a method chain (newline-per-chained-call) makes sense. Some places where it fails: Database migration files: exports.up = knex => {
return knex.schema
.table('midis', table => {
table.integer('plays').defaultTo(0).notNullable().index()
})
}D3 visualizations where you want to place methods on the same line for clarity: d3.select(info.selector)
.datum(info.data)
.transition().duration(500)
.call(chart)And sometimes it's just fine to put everything on one line: const first = crypto.createHash('sha1').update('first').digest()Even by adding a "magic number" to the rule (which we usually don't do because it's confusing) in the form of
|
HR commentedDec 13, 2016
•
edited
Hi @feross,
I would like to discuss adding a new rule: always break promise chains. Referring to the ES6 Promises (defined by the ECMA spec), breaking up promise chains makes the code much more readable. Consider the following.
So instead of writing a promise chain like this
It is far more cleaner, prettier and readable to break the promise chain like this with a new line and indentation.
I hope that you agree with me and it can become part of the standard rules.
Many thanks.