Rule proposal: no-await-with-operators
See original GitHub issueWhat category of rule is this? (place an “X” next to just one item)
[ ] Enforces code style [x] Warns about a potential error [ ] Suggests an alternate way of doing something [ ] Other (please specify:)
Not sure about the rule name but I think this would be a good thing to avoid in any circumstances as it is very surprising.
Jake Archibald explains it very well here https://twitter.com/jaffathecake/status/999610181452533760
So to sum it up this should warn:
let x = 0;
async function test() {
x += await 2;
console.log(x)
}
and this
let x = 0;
async function test() {
x = x + await 2;
console.log(x)
}
but this should be ok
let x = 0;
async function test() {
let two = await 2;
x = x + two;
console.log(x)
}
Why should this rule be included in ESLint (instead of a plugin)?
It’s not related to any library and can be a source for really confusing bugs. This is not a very well known Javascript feature and I think it should be considered to be a “bad part” which should be avoided.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:12 (9 by maintainers)
Top GitHub Comments
Interesting. In my understanding,
x
and it stores the result into memory.await y
, (off course it’s asynchronous and other codes can interrupt it), and it stores the result into memory.x
.So…, this is same as a popular problem in multi-threading software. Or we have often seen it in the explanation of the RDB transactions. It causes unintentional behaviors if others modified the
x
between 2. and 3…Clearer expression of what happens is:
I think that we can consider the following rule, “
no-non-atomic-update
” (I’m not sure what is the antonym of atomic):I can give a motivating example with totally reasonable JavaScript:
I doubt that most (even experienced) developers would be able to spot the bug here.
This just seems like a footgun and that there’s little benefit in allowing
await
to be used in these types of expressions.