question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

no-return-await must be aware of try/catch/finally

See original GitHub issue

no-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:closed
  • Created 7 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
not-an-aardvarkcommented, Nov 12, 2016

The intention of the no-return-await rule is to flag unnecessary return await statements where await could be removed without observably changing the behavior of the code*. When return await is used within a try statement, this is not the case.


*aside from the fact that the Promise will fulfill one tick sooner

1reaction
daltonescommented, Nov 12, 2016

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found