JavaScript

JavaScript

Made by DeepSource

Unnecessary return await function found JS-0111

Performance
Major

Returning an awaited value (like with return await f()) has two problems:

  • It queues an extra microtask, blocking the callstack until return is executed.

  • try blocks only catch a rejected promise if its awaited. return await may introduce unexpected hidden control-flow when handling errors.

Bad Practice

async function getUserByName(name: string) {
  // find() returns a Promise<User | null >.
  // This promise should be `await`ed by the caller of `getUserByName`.
  return await db.users.find({ userName: name })
}

Recommended

async function getUserByName(name: string) {
  // find() returns a Promise<User | null >.
  return db.users.find({ userName: name })
}

// OR:

async function getUserByName(name: string) {
  // If we must `await` the return-value in this function
  // it's better to do it this way. This is more performant:
  const user = await db.users.find({ userName: name })
  return user;
}

References