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 upConsistency in enforcing variable names? #550
Comments
This comment has been minimized.
This comment has been minimized.
ifraixedes
commented
Jun 18, 2016
|
I would prefer to enforce one name for the error callback functions, which should be |
This comment has been minimized.
This comment has been minimized.
|
I would personally prefer function example1 (fn) {
fn(null, 1)
}
function example2 (fn) {
fn(2)
}
example1(function (theError, theValue) { console.log(theValue) })
example2(function (theValue) { console.log(theValue) }) |
This comment has been minimized.
This comment has been minimized.
|
Right now, there's no ESLint rule for enforcing the name of the error callback. But we still want to detect if the programmer forgets to handle the error, so we're erring on the side of caution and ensuring that variables named In the future, if there's some way to enforce that |
This comment has been minimized.
This comment has been minimized.
|
As for the promise function names I tend to not use promises, so I'm not sure what the majority of the community is using these days. For promises, we do have the ability to enforce which function names are used, and I agree that Do any promise users have thoughts on this? |
feross
added
the
question
label
Jul 13, 2016
This comment has been minimized.
This comment has been minimized.
|
I use promises almost every day and I don't think I've ever come across |
This comment has been minimized.
This comment has been minimized.
|
Closing due to lack of feedback. There's nothing actionable here. |
feross
closed this
Aug 19, 2016
This comment has been minimized.
This comment has been minimized.
@feross missed this, apologies. The comment where I mentioned this was suggesting that perhaps standard should hold off on defining anything until the community has more time to experiment and come to a defacto standard. Explicit is nice at first but grows unnecessarily verbose as the community grows a shared understanding e.g. Also, somewhat in jest I've been preferring |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
trusktr
commented
Apr 17, 2018
•
|
Here's a reason why we should allow function createPromise() {
let resolve = () => {}
let reject = () => {}
const promise = new Promise( ( res, rej ) => { resolve = res; reject = rej } )
return { promise, resolve, reject }
}If I name them |
This comment has been minimized.
This comment has been minimized.
trusktr
commented
Apr 17, 2018
•
|
This helper makes it easy to write other code like this: function increaseWidthBy( selector: string, px: number ) {
const { promise, resolve } = createPromise()
window.addEventListener( 'load', () => {
addWidth( links, px )
resolve()
} )
return promise
}without having to write let resolve = () => {}
let reject = () => {}
const promise = new Promise( ( res, rej ) => { resolve = res; reject = rej } )all the time, and without having to nest code inside the executor function passed to |
This comment has been minimized.
This comment has been minimized.
|
@trusktr Whats wrong with having the code inside the constructor? function increaseWidthBy( selector: string, px: number ) {
return new Promise((resolve) => {
window.addEventListener('load', () => {
addWidth(links, px)
resolve()
})
})
}The idea with the revealing constructor pattern is that If you are running into this often, I think that you might be doing something that's not quite aligned with how others are doing it, and in that case I don't think that standard should change any rules. In your example, you also want to name the parameters let resolve = () => {}
let reject = () => {}
const promise = new Promise((...args) => { resolve = args[0]; reject = args[1] })tl;dr I think that enforcing |
zaynv commentedJun 18, 2016
•
edited
Just wanted to see if you had any thoughts on making the variable name enforcing a bit more consistent.
The rule called
handle-callback-errseems to throw an error when you have a variable callederrorerrorin your callback and you don't handle it. Example:The same thing happens if you use
erroras the variable name, but not anything else. So it looks like it is pushing you to use one of those variables.With Promises, there seems to be an inconsistency. When you use
resolveandrejectas the variable names, everything works fine:But if you use the three-letter shorthands
resandrej, (similar to theerrorvserr), you get a linting error from thepromise/param-namesrule that says "Promise constructor parameters must be named resolve, reject".Is it possible to make this more consistent? I don't really mind which one is used, but it would be preferable to enforce
errorinstead oferror otherwise allowerrand then also allowresandrejto be used.