[英]What to return from an async function in Firebase functions?
Inside the async function, do I need to return await for the function to complete correctly, or is the code below fine? 在异步函数内部,我是否需要返回等待状态才能使该函数正确完成,还是下面的代码正常?
const db = admin.firestore();
export const onUserCreate = functions.auth.user().onCreate(async event => {
try {
const user: User = {
userId: event.data.uid,
email: event.data.email
};
await db.doc(`users/${user.userId}`).set(user, {merge: true});
} catch (error) {
throw error;
}
});
This function already returns a promise that only resolves after your db.doc(...).set(..)
has itself resolved. 此函数已经返回了一个
db.doc(...).set(..)
,仅在您的db.doc(...).set(..)
自身解析后db.doc(...).set(..)
解析。 return await
would in this case be redundant. 在这种情况下,
return await
将是多余的。
So I think your code is fine as is, but it's simple enough that there is the possible improvement of forgetting about using async/await entirely, and just returning the promise directly. 因此,我认为您的代码可以正常使用,但是它很简单,可以完全忘记使用async / await,而直接返回promise可能会有所改善。 Eg:
return db.doc(...).set(...)
, without declaring the function as async. 例如:
return db.doc(...).set(...)
,而不将函数声明为异步。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.