简体   繁体   English

firebase 函数是否保证异步函数的执行顺序?

[英]Does firebase functions garantee order of execution for async functions?

I have a firebase function that does lot of checks in the firebase realtime database and then returns a response.我有一个 firebase function,它在 firebase 实时数据库中进行大量检查,然后返回响应。

Does the node runtime in firebase garantee that the async functions will be executed in the order they are call? firebase 中的节点运行时是否保证异步函数将按照它们被调用的顺序执行? or there is some sort of non fifo scheduler that executes then?还是有某种非 fifo 调度程序会执行?

the logic of the real function is a bit complex (over 200 lines) so to avoid the extra complexity i will just use a pseudo function as example:真正的 function 的逻辑有点复杂(超过 200 行),所以为了避免额外的复杂性,我将只使用伪 function 作为示例:

function checks(req,res){

let resp;
database.ref('nodeA').once('value').then(function(data) {

//do some checks and modify resp

});

database.ref('nodeB').once('value').then(function(data) {

//do some checks and modify resp

});

database.ref('nodeC').once('value').then(function(data) {

//do some checks and modify resp
res.status(200).send(resp);
});

FIRST OF ALL .首先 I know I can make nested calls to the realtime database and garantee the execution of all checks, but my real case scenario is more complex than this and would't work for me我知道我可以对实时数据库进行嵌套调用并保证执行所有检查,但我的实际情况比这更复杂,对我来说不起作用

Is there any garantee that all checks will be executed by this sample code?是否保证所有检查都将由该示例代码执行?

if not... how can i make a non blocking while that waits it to be ready?如果没有...我怎样才能在等待它准备好时进行非阻塞? like:像:

while(!resp.ready){
wait //how to wait without blocking the other functions
}
res.status(200).send(resp);

try async and await for this case, in your code, you will send the response to the user before finishing all the validation, there is no guarantee the callback function for each promise will execute in the same order.在这种情况下尝试异步和等待,在您的代码中,您将在完成所有验证之前向用户发送响应,不能保证每个 promise 的回调 function 将以相同的顺序执行。

async function checks(req,res){

let resp;
let nodeAData=await database.ref('nodeA').once('value');
//do some checks and modify resp

let nodebData=database.ref('nodeB').once('value')
//do some checks and modify resp

.
.
.
res.status(200).send(resp);
});

In the code you shared the order in which the three requests are sent to the database is in the order you specify them.在您共享的代码中,三个请求发送到数据库的顺序是您指定的顺序。 The results are also guaranteed to come in that same order.结果也保证以相同的顺序出现。

So by the time nodeC is loaded, it is guaranteed that the first two callbacks have also been invoked.因此,在加载nodeC时,可以保证前两个回调也已被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM