question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: no-await-with-operators

See original GitHub issue

What 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:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
mysticateacommented, May 28, 2018

Interesting. In my understanding,

// this is same as `x += await y`
x = x + await y
  1. It evaluates the 2nd x and it stores the result into memory.
  2. It evaluates the await y, (off course it’s asynchronous and other codes can interrupt it), and it stores the result into memory.
  3. It assigns the sum of 1. and 2. into the 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:

let local1 = x
let local2 = await y
x = local1 + local2

I think that we can consider the following rule, “no-non-atomic-update” (I’m not sure what is the antonym of atomic):

var outer = 0
var outerObj = {}

var outerFunc = () => {}

async function f(param) {
    //× BAD
    outer += await x
    outer = outer + await x
    outerObj.a = outerObj.a + await x
    outerObj.a = outerFunc() + await x
    param.a = param.a + await x
    param.a = outerObj.a + await x
    param.a = outerFunc() + await x
    outer = g(outer, await x)

    //× BAD maybe; I'm not sure if we should warn this case.
    const local1 = outer
    outer = local1 + await x

    //✔ GOOD; this is atomic
    outer = (await x) + outer
    outer = g(await x, outer)
}
2reactions
jamiebuildscommented, Jul 9, 2018

I’m not sure this warrants a rule; in the linked tweet, this only is “confusing” because the variable is being mutated both inside and outside the async function.

I can give a motivating example with totally reasonable JavaScript:

async function countFiles(dir) {
  let files = await readdir(dir);
  let count = 0;

  await Promise.all(files.map(file => {
    let filePath = path.join(dir, file);
    let fileStat = await stat(filePath);

    if (fileStat.isDirectory()) {
      count += await countFiles(file);
    } else {
      count += 1;
    }
  });

  return count;
}

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SEC Proposed Rules
SEC Proposed Rules · Regulation NMS: Minimum Pricing Increments, Access Fees, and Transparency of Better Priced Orders · File No: S7-30-22 · Comments...
Read more >
Proposed Rules - GovInfo
This proposed rule, if finalized, would harmonize certain sections of FDA's regulations on human subject protection and institutional review ...
Read more >
Agency Rule List - Spring 2022 - Reginfo.gov
Agency Agenda Stage of Rulemaking RIN SEC Proposed Rule Stage 3235‑AL91 SEC Proposed Rule Stage 3235‑AM06 SEC Proposed Rule Stage 3235‑AM78
Read more >
The Enhancement and Standardization of Climate-Related ...
The Commission has requested comment on a release proposing amendments to its rules under the Securities Act and Exchange Act that would require ......
Read more >
SEC Proposes Revisions to Shareholder Proposal Rules
The Securities and Exchange Commission (SEC) last month proposed revisions to Rule 14a-8 under the Securities Exchange Act of 1934, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found