从 firebase 云函数返回 null - 后台触发器
Returning null from firebase cloud function - background triggers
使用带有后台触发器的 firebase 云函数,如果没有处于挂起状态的承诺,Doug Stevenson 建议我们 return null
。以下是他建议的 2 个地方:
https://youtu.be/7IkUgCLr5oA?t=120
云函数中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 建议在我们没有 return 承诺时 returning null
。 null
有特殊待遇吗?
我发现您一定要听从 Doug 的建议并尝试 return null
,这是 why event-driven functions fail to complete
的原因之一
When functions written in Node.js return a rejected promise or pass a non-null value to a callback.
the function stops executing by default and the event is discarded.
您还需要 terminate your background functions 和 return 空值或使用其他 promise 方法。
If a function creates background tasks (such as threads, futures, Node.js Promise objects, callbacks, or system processes), you must terminate or otherwise resolve these tasks before returning from your function. Any tasks that are not terminated prior to returning from a particular execution may not be completed, and may also cause undefined behavior.
使用带有后台触发器的 firebase 云函数,如果没有处于挂起状态的承诺,Doug Stevenson 建议我们 return null
。以下是他建议的 2 个地方:
https://youtu.be/7IkUgCLr5oA?t=120
云函数中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 建议在我们没有 return 承诺时 returning null
。 null
有特殊待遇吗?
我发现您一定要听从 Doug 的建议并尝试 return null
,这是 why event-driven functions fail to complete
When functions written in Node.js return a rejected promise or pass a non-null value to a callback.
the function stops executing by default and the event is discarded.
您还需要 terminate your background functions 和 return 空值或使用其他 promise 方法。
If a function creates background tasks (such as threads, futures, Node.js Promise objects, callbacks, or system processes), you must terminate or otherwise resolve these tasks before returning from your function. Any tasks that are not terminated prior to returning from a particular execution may not be completed, and may also cause undefined behavior.