no-return-await must be aware of try/catch/finally
See original GitHub issueno-return-await
currently doesn’t consider if the return
statement is inside a try
block. In this case, the presence of await
does meter.
Consider this:
function asyncLog() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('first log');
resolve(true);
}, 1000);
});
}
async function foo() {
try {
return await asyncLog();
// I really need this await, but no-return-await is complaining about it :(
} finally {
console.log('second log');
}
}
foo();
// first log
// second log
async function bar() {
try {
return asyncLog();
} finally {
console.log('second log');
}
}
bar();
// second log
// first log
async function baz() {
try {
return asyncLog();
} catch (error) {
// if asyncLog() rejects, error will be swallowed because I missed await :(
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
"await" should not be used redundantly - Sonar Community
In Javascript/Typescript RSPEC-4326 it is declared that returning an awaited promise is redundant code smell.
Read more >TryCatchFinally (AWS SDK for Java - 1.12.367)
It means that cancellation request always waits for completion of all tasks that originate in these methods. Special care should be taken when...
Read more >Try...Catch...Finally statement - Visual Basic | Microsoft Learn
Must be implicitly convertible to Boolean . Any expression that describes a generic filter. Typically used to filter by error number.
Read more >space-after-keywords - ESLint Documentation - TypeError
This rule takes one argument. If it is "always" then the keywords must be followed by at least one space. If "never" then...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The intention of the
no-return-await
rule is to flag unnecessaryreturn await
statements whereawait
could be removed without observably changing the behavior of the code*. Whenreturn await
is used within atry
statement, this is not the case.*aside from the fact that the Promise will fulfill one tick sooner
The problem is that the warning message of this rule is “Redundant use of
await
on a return value”.The term “Redundant” is in fact incorrect in these
try
cases.