'How to run SELECT query ONLY AFTER the UPDATE queries have been completed in SQLite React Native?

I have a program that performs the following logic:

  1. Select all saved rows in SQLite
  2. Send saved rows via API
  3. Update rows based on API response (status = uploaded / failed)
  4. Check if all rows are success, if yes then go to the next page.
var db = openDatabase({ name: 'test.db' });
db.transaction(tx => {
    tx.executeSql(
        "SELECT * FROM row_details WHERE offline_status == 'created' OR offline_status == 'updated'",
        [],
        (tx, results) => {
             let rows = [], message = '', totalRows = results.rows.length;
             for (let i = 0; i < totalRows; i++) {
                 const row = results.rows.item(i);
                 // perform API and update query
                 var query = `UPDATE row_details SET offline_status = 'uploaded' WHERE id = ${row.id}`;
                 next.executeSql(query);
             }
        }
    );
    // I want this query to be executed only WHEN all of the above have been completed.
    tx.executeSql("SELECT * FROM row_details WHERE offline_status = 'uploaded'");
});

I want the last SELECT query be performed ONLY when the UPDATE queries have been completed. Currently, what happens is that both the queries run in parallel since tx.executeSql is async.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source