Synchronous query to Web SQL Database
I used callbacks and a closure to solve a similar problem, consider:
function getFolder(id, callback) {var data = [];ldb.transaction(function (tx) {tx.executeSql('SELECT * FROM folders where id=?', [id], function (tx, results) { if (results.rows && results.rows.length) { for (i = 0; i < results.rows.length; i++) { data.push(results.rows.item(i)); } } if (typeof(callback) == 'function') callback(data); }, function (tx, error) { console.log(error); });});}
In the continuation of this example, folder has a property parent to define it's relation to other folders. As does a document. The following will get you the path of a document using a closure (success):
function getDocPath(doc, callback) { var path = []; var parent = doc.parent; var success = function(folder) { var folder = folder[0]; parent = folder.parent; path.push({'id':folder.id,'name':folder.name}); if (parent != "undefined") getFolder(parent, success); else if ( typeof(callback) == 'function' ) callback(path.reverse()); } getFolder(parent, success); }
You could use callbacks with a closure to your stack of remaining queries. Or you could use recursion, passing the stack as parameters.