return await
function found JS-0111Returning 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 await
ed. return await
may introduce unexpected hidden control-flow when handling errors.
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 })
}
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;
}