By sfletche

2017-04-11 17:57:38 8 Comments

I see there is an eslint rule, no-return-await, for disallowing return await.

In the rule's description, it states a return await adds "extra time before the overarching Promise resolves or rejects".

However, when I look at MDN async function docs, the "Simple Example" shows an example containing return await without any description of why this might be a performance problem.

Is return await an actual performance problem as the eslint docs suggest?

And if so, how?


@Bergi 2017-05-15 16:59:58

No, there isn't any performance problem. It's just an unnecessary extra operation. It might take a bit longer to execute, but should be hardly noticeable. It's akin to return x+0 instead of return x for an integer x. Or rather, exactly equivalent to the pointless .then(x => x).

It doesn't do actual harm, but I'd consider it bad style and a sign that the author does not fully compre­hend promises and async/await.

However, there's one case where it make an important difference:

try {
    return await …;
} …

await does throw on rejections, and in any case awaits the promise resolution before catch or finally handlers are executed. A plain return would have ignored that.

@TigerBear 2018-08-22 14:43:18

IMO comparing it to return .then(x => x) is a bit harsh. As you pointed out, it is necessary in a try/catch. It would be maybe more like .then((x) => vs .then(x => I.E. in cases where you have multiple parameters, the parentheses around x are necessary, otherwise they are optional. I would view this as the same mindset with the debate being: Is it better to be consistent or to minimize?

@Bergi 2018-08-22 18:23:21

@TigerBear No, it's not harsh - it's exactly what is happening there. Writing return await … would be odd, just like await await … would be odd. It works, but is useless.

@TigerBear 2018-08-23 14:56:14

I wouldn't call it useless though since, as you already pointed out, it sometimes has a use, meaning it falls in a different category. Consistent vs Minimal. Similar to parentheses around arrow function parameters

@Bergi 2018-08-24 08:16:17

@TigerBear That's a very different construct that I am not calling useless, and there is no reason to consistently use return await in the different cases. await in a try block is equivalent to .then(x => x, err => …) or .catch(err => …) and is obviously not pointless.

Related Questions

Sponsored Content

8 Answered Questions

[SOLVED] Why do we need middleware for async flow in Redux?

33 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

21 Answered Questions

[SOLVED] How and when to use ‘async’ and ‘await’

9 Answered Questions

[SOLVED] Why does ++[[]][+[]]+[+[]] return the string "10"?

  • 2011-08-26 08:46:14
  • JohnJohnGa
  • 188025 View
  • 1525 Score
  • 9 Answer
  • Tags:   javascript syntax

7 Answered Questions

[SOLVED] How to safely call an async method in C# without await

12 Answered Questions

2 Answered Questions

[SOLVED] Node.JS - Can`t get async throws with try/catch blocks

1 Answered Questions

[SOLVED] async/await vs combining generators and promises?

1 Answered Questions

[SOLVED] Writing promise based HTTP request using Async/Await

2 Answered Questions

[SOLVED] JS async/await - why does await need async?

Sponsored Content