8000 [await-thenable] warn against passing non-promise values to promise aggregators (`Promise.all`, `Promise.allSettled`, `Promise.race`) · Issue #1804 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

[await-thenable] warn against passing non-promise values to promise aggregators (Promise.all, Promise.allSettled, Promise.race) #1804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
viestat opened this issue Mar 26, 2020 · 12 comments · May be fixed by #10163
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@viestat
Copy link
viestat commented Mar 26, 2020

Repro

{
  "rules": {
    "@typescript-eslint/await-thenable": "error"
  }
}
const value = 'value';
const createValue = () => 'value';

await Promise.all([value, createValue]);

Expected Result
The rule should show a linting error:

Unexpected `await` of a non-Promise (non-"Thenable") value  @typescript-eslint/await-thenable

Actual Result
No liniting error.

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 2.24.0
@typescript-eslint/parser 2.24.0
TypeScript 3.8.3
ESLint 6.8.0
node 12.10.0
npm 6.10.3
@viestat viestat added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Mar 26, 2020
@bradzacher
Copy link
Member

I am unable to reproduce this against master

image

Could you please provide a repro repo for this to help me investigate?

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for team members to take a look labels Mar 26, 2020
@viestat
Copy link
Author
viestat commented Mar 26, 2020

@bradzacher, Thank you for your quick reply, I will create the repro repo, In the meantime, from the image you shared I can see that you added this test case in the valid property, the issue is that the code should be invalid and throw an error.

@bradzacher
Copy link
Member

wait sorry, I misunderstood your OP.

Why should it be invalid?
Promise.all returns a Promise always, regardless of the input values:
image
Which means that it should always be awaited.

@viestat
Copy link
Author
viestat commented Mar 26, 2020

Why should it be invalid?
Promise.all returns a Promise always, regardless of the input values

You are right, While it is valid JavaScript to pass non-promise elements to Promise.all it seems to be inconsistent with this rule.
It would prevent code like the following to be written:

const promise1 = Promise.resolve();
const promise2 = Promise.resolve();
const test1 = (): void => {};

export const run = async (): Promise<void> => {
  await Promise.all([promise1, promise2, test1()]);
};

Here it makes no sense at all to use await for all the input values since one of them is not a promise.

@bradzacher bradzacher added enhancement: plugin rule option New rule option for an existing eslint-plugin rule and removed awaiting response Issues waiting for a reply from the OP or another party labels Mar 26, 2020
@bradzacher
Copy link
Member
bradzacher commented Mar 26, 2020

AHHHHHH okay i'm with you now. Sorry, slow morning.

You're saying that you want the rule to warn against passing non-promise values into the native promise aggregation functions (Promise.all, Promise.allSettled, Promise.race).

Even though it is type-safe and runtime-safe, it may not make logical sense to do so.

If yes, this sounds perfectly reasonable. Would be good as an option on the rule.

@viestat
Copy link
Author
viestat commented Mar 26, 2020

@bradzacher Precisely 😄

@bradzacher bradzacher changed the title [await-thenable] Unexpected await of a non-Promise does not work if wrapped with Promise.all [await-thenable] warn against passing non-promise values to promise aggregators (Promise.all, Promise.allSettled, Promise.race) Mar 26, 2020
@andreialecu
Copy link
andreialecu commented Apr 13, 2020 & 8000 #8226;

Refactored some code recently where I saw various calls like:

const someFn = () => {...};
await Promise.all([await someFn(), await someOtherFn()])

It seems this should also prevent that type of problem? Or is there an alternative?

@bradzacher
Copy link
Member

No, there are no rules to prevent this code. Please file a new issue, and happy to accept a PR

@andreialecu
Copy link
andreialecu commented Apr 13, 2020

I understand that, I meant that fixing this issue would additionally fix the use case I mentioned.

@bradzacher
Copy link
Member

Sorry, I guess that potentially it would be if the types work out (i.e. your functions don't return something weird like Promise<Promise<foo>>.

Though it wouldn't be warning against the fact that you have await foo in the Promise.all, it would instead warn against passing a non-promise into Promise.all - meaning the error messages will be relatively cryptic.

@andreialecu
Copy link

I think that if functions return Promise<Promise<foo>> then the usage is probably correct and there shouldn't be a warning.

I think the cryptic error would be ok, at least the user will get a clue that something is wrong with the code. Perhaps it could even provide a hint like did you already await the inner promise?

@JoshuaKGoldberg JoshuaKGoldberg added the accepting prs Go ahead, send a pull request that resolves this issue label Oct 25, 2021
Tjstretchalot added a commit to Tjstretchalot/typescript-eslint that referenced this issue Dec 18, 2023
Tjstretchalot added a commit to Tjstretchalot/typescript-eslint that referenced this issue Jan 2, 2024
8000 Tjstretchalot added a commit to Tjstretchalot/typescript-eslint that referenced this issue Jan 10, 2024
Tjstretchalot added a commit to Tjstretchalot/typescript-eslint that referenced this issue Jan 12, 2024
Tjstretchalot added a commit to Tjstretchalot/typescript-eslint that referenced this issue Jan 26, 2024
Tjstretchalot added a commit to Tjstretchalot/typescript-eslint that referenced this issue Feb 2, 2024
@hazae41
Copy link
hazae41 commented Feb 8, 2024

Days since last bug caused by this: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
5 participants
0