-2

Consider the code:

const getUsers = async () => {
    // get users from DB
    // and return 
}

Is there a difference between

const someOtherFunction = () => { 

    // do some stuff...
    const users = await getUsers();
    return users;   

}
  

And

const someOtherFunction = async () => { 

    // do some stuff...
    return await getUsers();
}
JAN
  • 21,236
  • 66
  • 181
  • 318
  • 2
    No there isn't (unless you would do additional things between assigning to `users` and returning it). – Felix Kling Jun 08 '21 at 13:31
  • The longer code is harder to understand because of the superfluous temporary variable. – Bergi Jun 08 '21 at 13:34
  • 1
    `return await` is quite often not useful anyways, where is the option for `return getUsers();`? – ASDFGerte Jun 08 '21 at 13:36
  • [Why no-return-await vs const x = await?](https://stackoverflow.com/questions/44806135) and [Are there performance concerns with `return await`?](https://stackoverflow.com/questions/43353087) – adiga Jun 08 '21 at 13:37

1 Answers1

0

Well, eslint has a no return await rule, where describes reason:

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.

rostegg
  • 88
  • 9