You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This, I guess, is more of a feature request than an issue:
It would be great to be able to report all the un-handled promises. Meaning: for every async-function call require an await before or .then() after.
Use case:
Consider an async function validating a username and returning Promise<boolean>.
Using this function it is easy to forget to use await before the it
// Error: this will always result in "Register user" because the promise
// object is getting implicitly converted to `true`
if (isUsernameValid(name)) {
// Register user
} else {
// Show error
}
Expected behavior: Report un-handled promises and require either await before
if (await isUsernameValid(name)) { ... }
or .then() - now the promise is not un-handled, this will indicate that the awaiting is not desired
function createUser() {
await createDbRecord();
// Not waiting for the async function to resolve, at the same time
// indication that we are aware that we are making an async call.
logRegistration().then();
}
JetBrains IDEs have this check but it would be great to catch this in CI
The text was updated successfully, but these errors were encountered:
Came here looking to find the history on just this issue. This is by far the most common bug I have seen with promises, particularly the Promise<void> case.
@jazzfog I think the last case you outlined (just calling a promise-returning function and doing nothing with the promise) is well caught by @typescript-eslint/no-floating-promises.
The other case is what brought me here, indeed a very frequent source of trouble: a promise (directly after being created, or via being assigned to a variable first) is handed to a boolean check of some sorts. I cannot imagine a case where this would actually be desirable, since it will always be truthy.
Some variations, assuming the async isUsernameValid from above:
// Ternary expression … always truereturnisUsernameValid(name) ? userByName(name) : null;// Save to a variable first … same problemconsthasValidUser=isUsernameValid(name);if(hasValidUser){…}// Return boolean value … dittoreturnisUsernameValid(name)&&isPasswordValid(password);
I'm not sure what JetBrains exactly means by where a value is expected. I've only had this problem so far with cases where a boolean is expected. Any hints to other cases highly appreciated.
@xjamundx@macklinu this has been open for quite some time. I'd be happy to draft a PR, if I can rely on some support from your side (would be my first eslint rule to implement …).
Description
This, I guess, is more of a feature request than an issue:
It would be great to be able to report all the un-handled promises. Meaning: for every async-function call require an
await
before or.then()
after.Use case:
Consider an
async
function validating a username and returningPromise<boolean>
.Using this function it is easy to forget to use
await
before the itExpected behavior: Report un-handled promises and require either
await
beforeor
.then()
- now the promise is not un-handled, this will indicate that the awaiting is not desiredJetBrains IDEs have this check but it would be great to catch this in CI
The text was updated successfully, but these errors were encountered: