I am attempting to utilize a recursive function where each function runs only after the previous one has completed. This is the code I have written:
var service = ['users', 'news'],
lastSync = {
'users' : false,
'news' : false
};
db.transaction(function (tx) {
lastSyncFunc(tx,service,0).then(function(){
console.log(lastSync);
});
});
function lastSyncFunc(tx,service,index){
deferred = $q.defer();
tx.executeSql("SELECT time FROM last_sync WHERE fService = ? ORDER BY id DESC LIMIT 1", [service[index]], function (tx, result) {
if (result.rows.length > 0) {
lastSync[service[index]] = result.rows.item(0).fTime;
}
return ++index<service.length ? lastSyncFunc(tx,service,index) : deferred.resolve();
});
return deferred.promise;
}
Currently, my program is returning false
for both lastSync.users
and lastSync.users
because this section is being executed before the function completes.