The question itself may seem a bit confusing, but I'll try to explain it below.
So I have multiple queries that have to be executed, all depending on the previous one.
Let's say I'm executing query A, when that's done, query B has to be executed by using the data query A retrieved. After query B retrieved it's data, query C has to be executed with that specific data.
But while query B is executing, a FOR-loop is doing it's job and executing query C multiple times (query B finds 3 results, so query C has to be executed 3 times during the life duration of query B).
In the code below, I can try to simulate the situation with a more simplified form of my existing code (less details):
first function to be executed is QueryA() with a click on a button:
function QueryA() {
db.transation(
function(tx) {
tx.executeSql('SELECT * FROM TableA',
function(tx, results) {
QueryB(results);
});
});
}
function QueryB(results)
{
for(var i = 0; i < results.rows.length; i++)
{
db.transaction(
function(tx) {
tx.executeSql('SELECT * FROM TableB WHERE FIELDB = ?, [results.rows.item(i)],
function(tx, result) {
QueryC(result);
});
});
}
}
function QueryC(result) {
for(var i = 0; i < result.rows.length; i++) {
console.log(results.rows.item(i).description); //description being a field
}
}
The real problem is that all these function are asynchronous .. meaning they will be executed before the data has been retrieved and resulting in errors (I have left out the errorhandlers in this example, they are present in my current code, I just wanted to explain my problem).
I have no clue how the callback functions work, because I need to pull this off. Any advice is welcome! (and please show it with some code, so I can understand how the solution might work).
Thank you!
And just for a reminder, I'm working with Javascript and SQLite. I know Javascript can be tough sometimes to work with, especially working with functions that take quite some time and executing others that are dependant on the previous one.
Aucun commentaire:
Enregistrer un commentaire