【发布时间】:2021-12-24 03:17:13
【问题描述】:
使用带有后台触发器的 firebase 云函数,如果没有处于待处理状态的承诺,Doug Stevenson 建议我们返回 null。以下是他建议的两个地方:
https://youtu.be/7IkUgCLr5oA?t=120
https://stackoverflow.com/a/52214464/188740
云函数中null有什么特殊处理吗?
例如,所有这些都应该正确终止:
export const onFooWrite = functions.firestore
.document('foos/{fooId}')
.onWrite((change, context) => {
// do nothing (i.e. returns undefined)
});
export const onFooWrite = functions.firestore
.document('foos/{fooId}')
.onWrite((change, context) => {
return someAsyncFunction(); // returns a promise
});
export const onFooWrite = functions.firestore
.document('foos/{fooId}')
.onWrite((change, context) => {
if (something) return; // returns nothing (i.e. undefined)
return someAsyncFunction(); // returns a promise
});
export const onFooWrite = functions.firestore
.document('foos/{fooId}')
.onWrite(async (change, context) => {
await someAsyncFunction();
// returns nothing AFTER the promise is resolved
});
我相信这些都很好,但 Doug Stevenson 建议在我们不返回承诺时返回 null。 null有特殊处理吗?
【问题讨论】:
-
T = 140s 在同一视频中 - “如果您返回任何其他类型的值(而不是 Promise),Cloud 函数将忽略它并终止该函数”。可能会返回一些对象,但由于它们是后台函数,它们不会被发送到任何地方,所以也许 null 似乎可以完成这项工作。
-
@Dharmaraj 并不意味着我们可以返回任何东西(不仅仅是
null),它会是一样的吗?通过扩展,我们可以简单地不返回任何东西(这将被解释为返回undefined)并且应该做同样的事情,不是吗?所以换句话说,null没有什么特别的,对吧?还在寻找确认????
标签: node.js firebase google-cloud-functions