Expo SQLite success function sychronous

#1

Hello, I am new to react native and expo. I was using Expo’s SQLite functionality but I was experiencing a problem with the success function that was called when the query completes. I have a function that runs a select query, and the code after the query in the function depends on receiving the information from the query which is only accesible in the success parameter. However, the success function only executes some time after the rest of the code. How do I wait for the success function to execute before continuing the code, or is there another way to extract the selected row information?
Example:
<Button title=“Press me” onPress={()=>{thisFunction(val); console.log(returnval)}}

defined later:
thisFunction = async (val) => {
//verifying val…
let selectedRows;
db.transaction(tx => {
tx.executeSQL(`select * from table where val = ?`, [val], (_,{rows})=>{selectedRows = rows;}, (_, err)=>{console.log(err)})
})
//do something with selectedRows once it is filled by success function
returnval = “Status message”//global variable used to get past async function return
}

#2

Hi there!

I think you need to use await. Please read this documentation.

I hope this will help you. Let me know if it does!

#3

Oops! That was a mistake. When typing my code here I forgot the async and await keywords. I had already done that and it still didn’t work because I don’t know how to ‘await’ the success function. Updated code:
<Button title=“Press me” onPress={()=>{thisFunction(val); console.log(returnval)}}

defined later:
thisFunction = async (val) => {
//verifying val…
let selectedRows;
awaitdb.transaction(async tx => {
await tx.executeSQL(select * from table where val = ?, [val], (,{rows})=>{selectedRows = rows;}, (, err)=>{console.log(err)})
})
//do something with selectedRows once it is filled by success function
returnval = “Status message”//global variable used to get past async function return
}

#4

My message was a little cryptic earlier… It still doesn’t work because I have no idea how to use await for the success function as the 3rd parameter in the executeSQL statement.

1 Like
#5

You might want to try code block formatting so its easier to read. If I use the formatting I will see:

thisFunction = async (val) => {
//verifying val…
let selectedRows;
awaitdb.transaction(async tx => {
await tx.executeSQL(select * from table where val = ?, [val], (,{rows})=>{selectedRows = rows;}, (, err)=>{console.log(err)})
})
//do something with selectedRows once it is filled by success function
returnval = “Status message”//global variable used to get past async function return
}

Where there is a space missing in between awaitdb that you may want to fix? There is also a (,{rows}) which looks illegal and also (, err) that won’t work.

#6

Sorry… Those all appear correctly in a text editor but I guess expo interprets some characters differently…
`<Button title=“Press me” onPress={()=>{thisFunction(val); console.log(returnval)}}

//defined later:
thisFunction = async (val) => {
//verifying val…
let selectedRows;
await db.transaction(async tx => {
await tx.executeSQL(select * from table where val = ?, [val], (_,{rows})=>{selectedRows = rows;}, (_, err)=>{console.log(err)})
})
//do something with selectedRows once it is filled by success function
returnval = “Status message”//global variable used to get past async function return
}`
This is how it was supposed to be when I typed it the first time.

#7

However, this code does not work because I do not know of a method to wait for the success function to finish executing first. I could not find anything in the documentation for this.

closed #8

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