I am having trouble with two functions because they rely on chaining Async Tasks. The clearAll is a click event which should empty the database then reload the page. Clear is a function from the database module (database.clear).
I would like to use JQuery Promises/Deffered if they are appropriate but I cant quite grasp them for this use case.
I have re-written the code following @Larry K’s answer
clearAll: function()
{
var refresh = function()
{
localStorage.clear();
sessionStorage.clear();
window.location.href = window.location.pathname;
console.log("feeling refreshed");
};
database.open();
//This is what I'd like to be able to do
//With Jquery or callbacks
$.when(database.clear()).then(refresh);
},
clear: function (callback, errorCallback)
{
var sql = "SELECT name FROM sqlite_master WHERE type='table' AND name != ?",
args = ["__WebKitDatabaseInfoTable__"];
var dbTableNamesResult = function (tx, result)
{
var dropSql = "";
for (var i = 0; i < result.rows.length; i++)
{
dropSql = "DROP TABLE IF EXISTS " + result.rows.item(i).name + "; ";
execute(dropSql);
}
};
execute(sql, args, dbTableNamesResult);
},
Having attempted to implement jQuery deferred myself the answer I was looking for was provided by zerkms’s answer to How can I write this as a jQuery deffered function