简体   繁体   中英

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.

Does the node runtime in firebase garantee that the async functions will be executed in the order they are call? or there is some sort of non fifo scheduler that executes then?

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 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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