#ESLint rules for identifying missing `await` within list context

14 messages · Page 1 of 1 (latest)

agile walrus
#

I've been using the no-floating-promises TypeScript ESLint rule to catch situations where I've missed an await (very common issue when using Playwright).

That helps identify situations like this very nicely:

myAsyncFunction();  // Missing await is identified

However, it does not identify situations like the following:

const foo = new RegExp(
    [
        "foo",
        this.myAsyncFunction(),  // Missing await is **not** identified
    ].join(".*"),
    "s",
)

I tried also using no-misused-promises, but that didn't appear to help in this context either.

Is there some other rule I can use to identify such problems?

agile walrus
#

Simplifying the above code a little further, it appears to be when I use [].join() that the rule stops working.

For example, this works:

const foo = [
    myAsyncFunction()  // Missing await is identified
];

This does not work:

const foo = [
    myAsyncFunction()  // Missing await is **not** identified
].join();
latent hatch
#

I mean you pretty much want to ban assigning any promise at that point

#

For the specific situation you could use a helper. In general though I don't know if there's a rule. Generally you catch missing await because you're assigning to a var or func that tells you. But obviously array type is just inferred from what you provide.

agile walrus
#

The problem for me was really that I'd been lulled into a false sense of security, given that 99% of the time no-floating-promises will identify when I've missed an await. It took me about 2 hours to find the problem I was having, which turned out to be the missing await in a construct like the above.

So I was just hoping there would be a 'magic' solution so that I don't get caught out by something similar again... but if there isn't, then there isn't 🙂

dry hemlock
#

@agile walrus Yeah, I suspect that's just not a way to do that - it'd be hard to ban that code without banning code like:

const tasks = [ myAsyncFunction(), somethingElse ];
await Promies.all(tasks);
#

Lint rules generally try to only operate on the local code, without trying to look too much at the context of how its being used.

#

I think realistically the most likely way this could be caught would be to try to detect .join() being called on things that aren't Array<string>

#

I'm not finding a lint rule for that, either. I imagine it'd be possible to write one, but it's also a fairly specific bug

ember gust
dry hemlock
agile walrus
#

Thanks for all the responses. Happy to accept that what I was doing was a bit of an edge-case - just happened to catch me out, that's all.

#

I suppose the moral of the story for me, is that even when using no-floating-promises, I shouldn't rule out that there might be a missing await in there somewhere when hitting a strange issue. I got there in the end, even if it took me a little longer than I would have liked.