return await
function found JS-0111await
on a return value 6 */
7const getGroup = async (req, res) => {
8 const { response, grpInstance } = req
9 return await res.json({ response: await grpInstance.read() })10}
11
12/**
await
on a return value 8 * @returns {Promise} The promise that resolves with the result of the create operation.
9 */
10 async create () {
11 return await this.fs.create(this.createStructure)12 }
13
14 /**
await
on a return value63 */
64 async update () {
65 const record = await this.read()
66 return await this.fs.update(67 updateData(68 [69 'name',70 'description',71 'organizations',72 'volunteers',73 'started_date',74 'expected_completing_date',75 'initiated_organization'76 ],77 [78 this.name,79 this.description,80 this.organizations,81 this.volunteers,82 this.started_date,83 this.expected_completing_date,84 this.initiated_organization85 ],86 record87 )88 )89 }
90}
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;
}