Home > Article > Web Front-end > Return Await in JavaScript: Performance Hit or Style Issue?
ESLint's no-return-await rule prohibits the use of return await. The rule warns that return await introduces an unnecessary delay before a Promise resolves or rejects. However, the MDN async function documentation provides examples of return await without any caveat about performance concerns.
Contrary to the ESLint rule's description, return await does not have significant performance implications. It may execute slightly slower than a plain return, but this difference is negligible in most scenarios.
While return await is not a performance problem, it is considered poor style. It indicates potential misconceptions about promises and async/await. A plain return accomplishes the same functionality without the extra operation.
However, there is one instance when return await makes a critical difference:
try { ... return await ...; } ...
await triggers the Promise to resolve before executing catch or finally handlers. A plain return would bypass this behavior, leading to incorrect handling of rejected Promises.
The above is the detailed content of Return Await in JavaScript: Performance Hit or Style Issue?. For more information, please follow other related articles on the PHP Chinese website!