Are Expo SQLite transactions sync or async?

Hi- This feels like a stupid question, but some examples appear to be sync and others use await.

I have a series of set-up steps that need to be sync, so that is my current goal.
Thank you!
Lili

Hi @ldwight! Can you link the examples you mention?

Hi @jesse~

This example appears to be sync (i.e., no evident promises or awaits): https://github.com/expo/sqlite-example/blob/master/App.js

lines 119-130 specifically executes 2 SQL statements - an insert and a read that expects the insert to have completed.

This code example is async:

in lines 27-32 it appears as though the author wants sync behavior and is trying to make it using await…

The documentation says:
A Transaction object is passed in as a parameter to the callback parameter for the db.transaction() method on a Database (see above). It allows enqueuing SQL statements to perform in a database transaction. It supports one method: …

So… my question is, is a db.transaction async or sync? I’m probably missing something in the documentation, but have read it a few times, as well as looking for examples.

Thank you for any help you can give me!

Hi Jesse,
Sorry to be a noodge, but if you can’t answer the question, do you have a suggestion of someone who could?
Thanks,
Lili

Neither of those examples expect the sqlite methods to return a promise. In the snack that you linked, the author has this code

  executeSql = async (sql, params = []) => {
    return new Promise((resolve, reject) => db.transaction(tx => {
      tx.executeSql(sql, params, (_, { rows }) => resolve(rows._array), reject)
    }))
  }

that wraps the sqlite transaction in a promise. The transaction method takes callbacks as explained here: https://docs.expo.io/versions/latest/sdk/sqlite.html#dbtransactioncallback-error-success.

4 Likes

I guess I didn’t answer your question directly :slight_smile:. db.transaction is asynchronous but it takes callbacks instead of returning a promise. You can enqueue statements in a transaction and those are guaranteed to run in order which is why this works:

lines 119-130 specifically executes 2 SQL statements - an insert and a read that expects the insert to have completed.

3 Likes

Thank you!!! That clarifies it nicely!

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