简体   繁体   English

如何从 Firebase 云调用异步函数 Function

[英]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.

相关问题 Firebase Cloud Functions - 如何调用另一个函数并返回对象 - Firebase Cloud Functions - How to call another function and return object 如何从 firebase 云 function 返回数据并返回异步响应? - How to return data from firebase cloud function and return async response? 如何从 React stream 聊天应用程序调用 Firebase 云 function - How to call Firebase cloud function from React stream chat app flutter firebase 云函数模拟器,随叫随到的函数不起作用 - flutter firebase cloud function emulator, on call functions not working Firebase 在手机上如何验证对云 function 的调用? - Firebase on mobile how to authenticate call to cloud function? Firebase Cloud Functions - 从 https 中的上下文获取来源可调用 function - Firebase Cloud Functions - get origin from context in https callable function 与 firebase 云函数中的 firebase firestore 交互 - Interacting with firebase firestore from firebase cloud functions Firebase 云功能:部署 function 时出错 - Firebase Cloud Functions: Error while deploying function 如何从 Cloud Functions 访问和操作 Firebase 数据库? - How can I reach and manipulate Firebase Database from Cloud Functions? 如何从云函数中的请求中获取参数? 火力地堡 - How to get parameters from request in cloud functions? Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM