return 值在 Firebase Cloud Functions 中重要吗
Is return value important in Firebase Cloud Functions
我正在使用 TypeScript 编写 Firebase Could Functions,以下是更新文档的简单方法。
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
export const handleTestData = functions.firestore.document('test/{docID}').onCreate(async (snap, context) => {
const data = snap.data();
if (data) {
try {
await admin.firestore().doc('test1/' + context.params.docID + '/').update({duplicate : true});
} catch (error) {}
}
});
在此方法中,promise 由 async
await
处理,没有 return
语句,并且工作正常。我见过的大多数examples/tutorials在每个方法中总是有一个return
语句。
Firebase Cloud Functions 中有 impact/difference 我没有 return 的任何东西吗?如果我应该return某事,我可以returnnull
吗?
Is return value important in Firebase Cloud Functions?
是的,它确实是 key,在 Cloud Function 中执行异步处理(也称为“后台函数”)到 return a JavaScript 在所有异步处理完成时承诺,如 documentation.
中所述
这样做很重要,主要有两个原因(文档摘录):
- 您确保您的 Cloud Functions 实例 运行 在您的函数成功达到其终止条件或状态之前不会关闭。
- 您可以避免 运行 过长或无限循环的 Cloud Functions 过度收费。
为什么即使您没有 return Promise,您的 Cloud Function 运行 也会正确?
通常您的 Cloud Functions 应该在异步操作完成之前终止,因为您没有 return Promise,因此向 Cloud Functions 平台表明它可以终止 Cloud Functions 实例 运行启用云功能。
但有时,Cloud Functions平台不会立即终止Function,异步操作可以完成。这根本无法保证并且完全不受您的控制。
经验表明,对于简短的异步操作,后一种情况经常发生,开发人员认为一切正常。但是,突然之间,有一天,Cloud Function 无法正常工作......有时它确实有效:开发人员面临着一种没有任何清晰逻辑的“不稳定”行为,这使得调试变得非常困难。你会在 Stack Overflow 中找到很多问题来说明这种情况。
具体来说,在您的情况下,您可以像这样调整代码:
export const handleTestData = functions.firestore.document('test/{docID}').onCreate(async (snap, context) => {
const data = snap.data();
if (data) {
try {
// See the return below: we return the Promise returned by update()
return admin.firestore().doc('test1/' + context.params.docID + '/').update({duplicate : true});
} catch (error) {
return null; // <- See the return
}
} else {
return null; // <- See the return
}
});
或喜欢
export const handleTestData = functions.firestore.document('test/{docID}').onCreate(async (snap, context) => {
const data = snap.data();
if (data) {
try {
await admin.firestore().doc('test1/' + context.params.docID + '/').update({duplicate : true});
return null; // <- See the return
} catch (error) {
return null; // <- See the return
}
} else {
return null; // <- See the return
}
});
返回 null
(或 true
,或 1
...)是有效的,因为 async
函数总是 return 是一个 Promise。
我正在使用 TypeScript 编写 Firebase Could Functions,以下是更新文档的简单方法。
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
export const handleTestData = functions.firestore.document('test/{docID}').onCreate(async (snap, context) => {
const data = snap.data();
if (data) {
try {
await admin.firestore().doc('test1/' + context.params.docID + '/').update({duplicate : true});
} catch (error) {}
}
});
在此方法中,promise 由 async
await
处理,没有 return
语句,并且工作正常。我见过的大多数examples/tutorials在每个方法中总是有一个return
语句。
Firebase Cloud Functions 中有 impact/difference 我没有 return 的任何东西吗?如果我应该return某事,我可以returnnull
吗?
Is return value important in Firebase Cloud Functions?
是的,它确实是 key,在 Cloud Function 中执行异步处理(也称为“后台函数”)到 return a JavaScript 在所有异步处理完成时承诺,如 documentation.
中所述这样做很重要,主要有两个原因(文档摘录):
- 您确保您的 Cloud Functions 实例 运行 在您的函数成功达到其终止条件或状态之前不会关闭。
- 您可以避免 运行 过长或无限循环的 Cloud Functions 过度收费。
为什么即使您没有 return Promise,您的 Cloud Function 运行 也会正确?
通常您的 Cloud Functions 应该在异步操作完成之前终止,因为您没有 return Promise,因此向 Cloud Functions 平台表明它可以终止 Cloud Functions 实例 运行启用云功能。
但有时,Cloud Functions平台不会立即终止Function,异步操作可以完成。这根本无法保证并且完全不受您的控制。
经验表明,对于简短的异步操作,后一种情况经常发生,开发人员认为一切正常。但是,突然之间,有一天,Cloud Function 无法正常工作......有时它确实有效:开发人员面临着一种没有任何清晰逻辑的“不稳定”行为,这使得调试变得非常困难。你会在 Stack Overflow 中找到很多问题来说明这种情况。
具体来说,在您的情况下,您可以像这样调整代码:
export const handleTestData = functions.firestore.document('test/{docID}').onCreate(async (snap, context) => {
const data = snap.data();
if (data) {
try {
// See the return below: we return the Promise returned by update()
return admin.firestore().doc('test1/' + context.params.docID + '/').update({duplicate : true});
} catch (error) {
return null; // <- See the return
}
} else {
return null; // <- See the return
}
});
或喜欢
export const handleTestData = functions.firestore.document('test/{docID}').onCreate(async (snap, context) => {
const data = snap.data();
if (data) {
try {
await admin.firestore().doc('test1/' + context.params.docID + '/').update({duplicate : true});
return null; // <- See the return
} catch (error) {
return null; // <- See the return
}
} else {
return null; // <- See the return
}
});
返回 null
(或 true
,或 1
...)是有效的,因为 async
函数总是 return 是一个 Promise。