Code explanation:
If internet is available it will sync the unsynced data from the different tables based on different methods.But, I would like to know how to add promise for these different functions.The logic which I used in making all the functions to start simultaneously but, I would like to start function2 based on the success of function1 and repeat the same process can anyone tell me how to do it.
function syncApp () {
$log.log('offlineOnlineSync got called');
Contact.syncApp().then(function (resp) {
$log.log('Contact sync got called', resp);
WorkerGroup.syncApp().then(function (resp) {
$log.log('WorkerGroup sync got called', resp);
Category.syncApp().then(function (resp) {
$log.log('Category sync got called', resp);
Vehicle.syncApp().then(function (resp) {
$log.log('Vehicle sync got called', resp);
Daybook.syncApp().then(function (resp) {
$log.log('Daybook sync got called', resp);
}, CommonService.errorHandler);
}, CommonService.errorHandler);
}, CommonService.errorHandler);
}, CommonService.errorHandler);
}, CommonService.errorHandler);
}
In the first method the process which takes place is this but before completing the above process the second method gets called.
prom = DB.update('contacts', servResp, 'id', key)
.then(function () {
if (servResp.type === 'Worker') {
WorkerGroup.checkGroupForTempIds(key, servResp.id)
.then(function (resp) {
$log.log('Response in contact service', resp);
}, function (err) {
$log.log('err: ', err);
});
} // fix me // not needed
}, function (err) {
$log.log('err: ', err);
});
$log.log('serverresponseid', servResp.id);
$log.log('key', key);
var daybook_updatequery = 'UPDATE daybook SET contact_id = ? WHERE contact_id = ?';
$cordovaSQLite.execute(Database.db, daybook_updatequery, [servResp.id, key])
.then(function (resp) {
$log.log('response', resp);
defer.resolve('success');
}, function (err) {
q.reject(err);
$log.log(err);
});
proms.push(prom);
});
$q.all(proms).then(function () {
defer.resolve('success');
});