return await
function found JS-0111await
on a return value124 },
125 );
126
127 return await res.json();128}
129
130/**
await
on a return value102 body: JSON.stringify(body),
103 });
104
105 return await res.json();106}
107
108/* The code below does the following:
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;
}