I am using jQuery promises to return the data set from a web SQL database table and using $.when
, I can return the data set and use it inside $.when
according to this link
Return multiple rows as an array from a WebSQL databse using javaScript
But how can I use that data set outside $.when
?
Here is the source for query:
function getContacts(){
var defer = $.Deferred();
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM contacts', [], function(tx, results) {
defer.resolve(results.rows);
},function(tx, err){
alert("An internal error occurred. " + err.message);
});
});
return defer.promise();
}
Here is the source for returning and using that value inside $.when
:
var contact = getContacts();
$.when(contact).done(function(cont) {
for(var i = 0; i < cont.length; i++){
// can use the 'cont' variable to access data in the table
// inside this '$.when', but can't use it outside this $.when block
}
});
How can I achieve this?