JSFiddle - React, Tailwind, and code Playground
by xalvin11
HTML
<button id="test">TEST</button>
JavaScript
function getContacts(){
var db = openDatabase('sampleDB', '1.0', 'Sample DB', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS Employee (id INTEGER PRIMARY KEY, firstName, lastName, idNo UNIQUE, status)');
});
db.transaction(function (tx) {
tx.executeSql('INSERT INTO Employee (firstName, lastName, idNo, status) VALUES ("Alvin", "Magalona", "041", "ACTIVE")');
});
var defer = $.Deferred();
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM Employee', [], function(tx, results) {
var data = [];
if (results.rows.length > 0) {
for (var i = 0; i < results.rows.length; i++) {
data.push(results.rows.item(i));
}
}
defer.resolve(data);
},function(tx, err){
alert("An internal error occurred. " + err.message);
});
});
return defer.promise();
}
$('#test').click(function () {
var fetchContacts = getContacts();
var contacts = [];
$.when(fetchContacts).then(function (cont) {
contacts = cont;
}).done(function () {
alert(contacts.length);
});
});