digitalbazaar / eslint-config-digitalbazaar

BSD 3-Clause "New" or "Revised" License
2 stars 1 forks source link

Throw error on "return await" outside of try/catch. #43

Closed mandyvenables closed 4 years ago

aljones15 commented 4 years ago

This seems like an improvement so thank you:

https://eslint.org/docs/rules/no-return-await

Using return await inside an async function keeps the current function in the call stack until the Promise that is being awaited has resolved, at the cost of an extra microtask before resolving the outer Promise. return await can also be used in a try/catch statement to catch errors from another function that returns a Promise.

You can avoid the extra microtask by not awaiting the return value, with the trade off of the function no longer being a part of the stack trace if an error is thrown asynchronously from the Promise being returned. This can make debugging more difficult.

we also might want to spread the knowledge that this leads to better stack traces:

async function foo() {
    try {
        return await bar();
    } catch (error) {}
}
davidlehn commented 4 years ago

I think this is fine since "return await" usually isn't desired. @aljones15 I'm not sure what your stack trace example is doing. Note that modern v8 (and maybe other runtimes) are pretty good at optimizing variations of these patterns and providing useful stack traces. You don't usually need to jump through hoops to do so.