How to wait on the return value from callback function in a sqlite transaction call?

Please provide the following:

  1. SDK Version: 46.0.0
  2. Platforms(Android/iOS/web/all): Android
  3. Add the appropriate “Tag” based on what Expo library you have a question on.

Hi All,

I have been struggling with this for some time now.
I call the following function from my native return code :

<Text style={[styles.Progress,{fontSize:20}]}>
    {calcSessionTime(TrainingPassID)} min.
</Text>

Code :

function calcSessionTime(TrainingPassID){
let time=0;
userDatabase.transaction((tx) => {
    tx.executeSql('SELECT sum(TrainingSession.SessionTime)/60 as sessiontime
      FROM TrainingSession WHERE TrainingSession.TrainingPassID=?',              
      [TrainingPassID],(_, { rows: { _array } }) => {

    console.log("Sessiontime :" + _array[0].sessiontime);
    time = _array[0].sessiontime; 
    return time;         
    }        
);      
});

console.log("return : " + time);
return time;    
};

I query a SQLIte database, which return an array with the sessiontime.

I have defined a variable “time” which is intially set to “cero”. This variable is assigned the Result from the query.

For some reason the returned value is still the initial value ???

I have tried to print the “time” out and it seems to be correct but not when the return line is reached ???

Output :

return : 0
Sessiontime : 6

It seems that callback is running as a async function, because the “return” value comes before the “sessiontime” value.

I have tried to figure out how it is possible to wait for the callback but is loost ???

How can this be done ?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.