-
Notifications
You must be signed in to change notification settings - Fork 887
Rule suggestion: require await
of async functions
#892
Comments
I'm not sure off the top of my head what exactly you'll need to do, but atom-typescript has a cool AST viewer which helps a lot when trying to write lint rules (see image below). However, I'm not sure this is possible at the moment. TSLint lints files on a file by file basis currently, so it your function comes from another file, it has no way of knowing that it's an async function. If you're only dealing with function declarations and calls from the same file, I imagine you could do something like this: Override |
blocked on #680 |
We just hit a bug where we weren't properly awaiting an One addition to the rule: it'd be nice if the rule automatically saw if you were legitimately wanted to run something in parallel, not async function foo() { ... }
async function bar() {
// This should fail:
foo();
// But these should pass:
await foo();
foo().then(
() => { ... },
(err) => { ... }
)
foo()
.catch((err) => { ... });
// Maybe ideally this should fail too, since it could lead to silent errors:
foo().then(() => { ... });
} |
await
of async functions?await
of async functions
yep, this feature request is now unblocked, accepting PRs |
@JoshuaKGoldberg @cevek I still suspect this is resolved by #1632. See my comment: #1632 (comment) |
Agreed. I couldn't think of any situations in which #1632's didn't catch this behavior. Thanks for the bump! |
Here's one situation that's not covered. I've just hit a bug as follows: let value = some_async_function(); // forgot to await!
if (value === null) {
// will never happen
} else {
// will always happen
} This is not the first time, and not the last. I propose a |
@tomholub have you tried enabling |
@tomholub I think more better to disable primitive operations, to boolean checks and check against null and undefined It is cover all buggy cases async foo() {
var b = bar(); // Promise.resolve();
if (b) some();
b ? some() : some();
x(!b);
x(+b);
x(~b);
x(b + 'foo');
if (b == null) x();
if (b === null) x();
if (b === undefined) x();
Promise.all([b]); // ok
someWithPromiseArg(b); // ok
} |
@JoshuaKGoldberg |
I wonder why the "no-floating-promises" rule isn't catching this case? 😢 Here is an example to play with: function awaitMe(): Promise<boolean> {
return new Promise(resolve => {
setTimeout(() => {
resolve(false);
}, 2500);
});
}
function isItTrue(): boolean {
return awaitMe() ? true : false;
}
console.log('isItTrue', isItTrue()); // It's always true because there is no await on "awaitMe" |
@bennyn This section is technically not a floating promise, since you handle it: awaitMe() ? true : false
This is a common concern brought up - a little more +1 to making |
function awaitMe() {
return new Promise((resolve, reject) => {
resolve();
});
}
awaitMe(); // gets warning from no-floating-promise
const value = awaitMe(); // doesn't get any warning
Any solution to handle this? |
For example:
I need just simple example how to start, find method declaration and check that is async function
The text was updated successfully, but these errors were encountered: