return await
function found JS-0111await
on a return value131 } else if (!quantity) {
132 throw new Error('Quantity is required')
133 } else if (quantity <= 0) {
134 return await this.removeItemFromGuestCart(sessionId, itemId)135 }
136 let guestCart = await GuestCart.findOne({ sessionId })
137 if (!guestCart) {
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 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;
}