0

I'm not sure if this will be a redundant or stupid question, but I didn't find a satisfying answer yet.

I want to do sql queries in Javascript. I want to achieve that the inserts are completely executed, when I start with selects. Now the problem is that I have to that in a loop and I don't know how to ensure that all queries have been finished.

in pseudocode, it looks like this.

for (var i = 0; i < N; i++) {
  db.transaction(function(tx) {
    tx.executeSql("INSERT ...", [], success, error);
  }, error, success)
}
db.transaction(function(tx) {
  tx.executeSql("SELECT ...", [], success, error);
});

If I just did two transaction, I could do the second in the callback of the first, but in a loop that seems not that easy.

Is there an elegant way to achieve my goal, without too much magic?

P.S. I can't do all queries in one transaction, because later inserts are dependent on previous ones, and there is a post request inbetween that takes a little time.

Thank you for reading my question.

Odin
  • 677
  • 3
  • 15
  • 1
    Since they are all promises you can create something that calls a callback function if all promises are resolved (keeping track of the number of calls vs the number of promises being resolved). You can also look at exisiting solutions or this like q.all(); – Bas Slagter Aug 17 '15 at 14:53
  • check this you: http://stackoverflow.com/questions/26778289/using-jquery-deferred-and-done-on-asynchronous-sqlite-database-queries-in-a-loop (`promises` is the keyword here) – Cristi Pufu Aug 17 '15 at 15:01

0 Answers0