Function Not Called. Anyone know why not?

I’m trying to return an integer from a function, but it always returns as null. It seems the function isn’t being called. Clearly I’m doing something wrong. Anyone know why this could be?
Thanks



totalPrizeFund = CalculatePrizeFundFromLeaderBoardEntries();



//TODO...this isnt working!
    async function CalculatePrizeFundFromLeaderBoardEntries()
    {
      
      var queryBuilder = Backendless.DataQueryBuilder.create();
      queryBuilder.setSortBy( [ "score DESC" ] );
      //set a string to the top ten leaders
      var count=0;
      //for( var i in result )
      
 
      await Backendless.Data.of( "Leaderboard_Game01" ).find(queryBuilder )
      
     .then( function( result ) {
         var totalFund=0;
                
         for( var i=0;i<result.length;i++ )
            {
                
             //if there is such an entry
               if(result!=null)
               {
                totalFund += result.costPaidPerScoreEntry;
               }
              
            }
            totalFund=100;//doenst work
          return totalFund;
      })
     .catch( function( error ) {
      });
     
    }

Hi, Simon.

  1. It is not clear for me what is the field ‘result.costPaidPerScoreEntry’ (line 26). result - is the array of objects. You can try to output to console the current object like console.log(result). And you’ll see every object from result.
  2. Then you rewrite value totalFund=100; and losе previous value;
  3. Also you use await for method .then() and ignore result. It is right behavior?

Yeah, I put in totalfund=100 just to see if anything at all was happening (it basically overwrites everything before it). This is just a debug line. totalPrizeFund still ends up as null.

So basically…even if I do this, it still returns null…


 async function CalculatePrizeFundFromLeaderBoardEntries()
    {
      totalFund=100;
      return totalFund;
}

…ah but it seems that if I remove the ‘async’ then it works…

Ii still dont understand what is happening. None of the Backendless.Data.of ever gets run but the test() function at the end runs perfectly well and prints the totalPrizeFund variable as altered by the initial randomize.

async function CalculatePrizeFundFromLeaderBoardEntries()
 {
 totalPrizeFund=0;
 
 //this works fine///////
 for( var x=0;x<100;x++ )
 { 
 totalPrizeFund += Math.floor((Math.random() * 100) + 1);
 }
 ////////////////////////
 
 //NONE OF THIS GETS TRIGGERED///////////////////////////
 Backendless.Data.of( "Leaderboard_Game01" ).find()
 .then( function( result ) {
 
 //does not run
 totalPrizeFund = 99999999999;
 
 })
 .catch( function( error ) {
 });
 ///////////////////////////////////////////////////////
 
 //but then this triggers fine and prints the value of totalPrizeFund which was calculated by the initial math.random (ie not the 99999999999)
 test();
 
 }

I know its something to do with blocking vs non blocking but I dont think I fully understand the concept /usage and cant find any useful help anywhere. Instead I have tried manually changing all the different permutation of async/await but I just cant get it to work. Apologies for my ignorance.

Your function is marked as “async”. It means when you invoke like this:

totalPrizeFund = CalculatePrizeFundFromLeaderBoardEntries();

The program will not wait for the result of the function to assign to the “totalPrizeFund” variable. The function is off to be executed in its own “parallel” universe. The main program will not wait for it.