title: Cordova SQLite wait until insert finishes
body: <p>I have multiple INSERTs I'd like to be done before starting SELECT requests. My problem is that the INSERT is not yet finished when the SELECT fires.</p>
  
  <p>Made a factory for the database handling:<br>
  databaseFactory.js<br></p>
  
  <pre><code>factory.insertIntoTable= function (database, data) {
    var sqlCommand = 'INSERT INTO blablabla';
  
    database.transaction(function(tx) {
      database.executeSql(sqlCommand, [data.thingsToInsert], function (resultSet) {
        console.log('success: insertIntoTable');
      }, function (error) {
        console.log('SELECT error: ' + error.message);
      });
    }, function(error) {
      console.log('transaction error: ' + error.message);
      database.close();
    }, function() {
      console.log('transaction ok: insertIntoTable');
    });
  };
  </code></pre>
  
  <p>app.js <br></p>
  
  <pre><code>ionic.Platform.ready(function () {
    if (window.cordova &amp;&amp; window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    };
  
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  
    db = window.sqlitePlugin.openDatabase({name: 'myDbName.db', location: 'default'});
  
    if (window.Connection) {
      if (navigator.connection.type !== Connection.NONE) {
        databaseFactory.createTables(db);
  
        MyService.getStuffFromAPI().then(function(result) {
          for (var index = 0; index &lt; result[0].campaigns.length; index++) {
            databaseFactory.insertIntoTable(db, result[0].campaigns[index]);
          }
  
          var selectResult = databaseFactory.selectFromCampaigns();
  
          console.log(selectResult); //This log comes earlier than the above inserts could finish.
  
        }, function(result) {
          console.log(result);
        });
      }
    }
  });
  </code></pre>
  
  <p>The INSERT is working fine anyways, I did check it.</p>
  
  <p>I know that the datebase.transaction is asynchronous so also tried it with single db.executeSQL command but had the same problem even with adding $q resolve, reject to the factory. I really could use some help, thanks!</p>