[英]How to call async functions from a Firebase Cloud Function
I'm exporting a Firebase cloud function that I can call from my iOS app.我正在导出一个 Firebase 云 function,我可以从我的 iOS 应用程序调用它。
exports.myFunction = functions.https.onCall((someData, context) => {
});
How do I call an async function?如何调用异步 function?
exports.myFunction = functions.https.onCall((someData, context) => {
return await someAsyncFunction();
});
The documentation states to return a promise but I'm not sure how to wrap an existing async function into a promise that I can return.文档声明要返回 promise,但我不确定如何将现有的异步 function 包装到我可以返回的 promise 中。
https://firebase.google.com/docs/functions/callable https://firebase.google.com/docs/functions/callable
To return data after an asynchronous operation, return a promise. The data returned by the promise is sent back to the client.异步操作后返回数据,返回一个promise,promise返回的数据返回给客户端。 For example, you could return sanitized text that the callable function wrote to the Realtime Database:例如,您可以返回可调用对象 function 写入实时数据库的经过清理的文本:
Try appending async in front of function to use await.尝试在 function 前面附加 async 以使用 await。
exports.myFunction = functions.https.onCall(async(someData, context) => { return await someAsyncFunction(); });
You can either make the callback function passed on onCall
async so you can use await inside it:您可以使回调 function 传递给onCall
异步,以便您可以在其中使用 await :
exports.myFunction = functions.https.onCall(async (someData, context) => {
return await someAsyncFunction();
});
Or, you can simply return the promise returned by someAsyncFunction
:或者,您可以简单地返回 someAsyncFunction 返回的someAsyncFunction
:
exports.myFunction = functions.https.onCall((someData, context) => {
return someAsyncFunction();
});
In either case, you are obliged to return a promise that resolves with the data to serialize and send to the client app.在任何一种情况下,您都必须返回一个 promise来解析数据以序列化并发送到客户端应用程序。 Both of these methods will do that.这两种方法都可以做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.