node.js – NodeJS race condition when retrieving Firebase realtime database node data

Having an issue with the following nodejs race condition (i believe it called).

I am trying to get the Pool ID <current #> is a

Below is my code and below that is the output:

for(;currPool<=lastPool;) {

    console.log("Current Pool ID: " + currPool);

    poolsRef.child(currPool).child("PoolType").once('value', function(snapshot) {
        
        if (snapshot.exists()) {
        
            const poolType = snapshot.val();
            //
            console.log("Pool ID:", currPool, "is a:", poolType , " Style PoolType");

         }

    });

    currPool++;

}

The output is as follows:

Current Pool ID: 11018
Current Pool ID: 11019
Current Pool ID: 11020
Current Pool ID: 11021
Current Pool ID: 11022
Current Pool ID: 11023
Pool ID: 11024 is a: Survivor  Style PoolType
Pool ID: 11024 is a: Pickem  Style PoolType
Pool ID: 11024 is a: Pickem  Style PoolType
Pool ID: 11024 is a: Pickem  Style PoolType
Pool ID: 11024 is a: Pickem  Style PoolType
Pool ID: 11024 is a: Survivor  Style PoolType

How do i get it to display :

Pool ID: 11018 is a: Survivor  Style PoolType
Pool ID: 11019 is a: Pickem  Style PoolType
Pool ID: 11020 is a: Pickem  Style PoolType
Pool ID: 11021 is a: Pickem  Style PoolType
Pool ID: 11022 is a: Pickem  Style PoolType
Pool ID: 11023 is a: Survivor  Style PoolType

Read more here: Source link