Sunday, January 6, 2019

javascript - Before completing the loop promise gets resolved

I am using a loop to update data to the server and once the response from the server is received it is updated in my local db.But before completing all the updates in local db the promise gets resolved and only one data gets updated. can anyone tell me why the promise gets resolved before completing all the process inside a for loop?



function updatecategories (data, categoryname) {

var defer = $q.defer();
var proms = [];
var prom;
$log.log('Change in daybook change got called', data);
$log.log('Change in daybook change got called', categoryname);
for (var i = 0; i < data.length; i++) {
delete data[i].is_synced;
data[i].category = categoryname;
save(data[i]).then(function (result) {
$log.log('values changed in daybook', result);

proms.push('success');//only one data is getting synced to server rest is not by nid on 10-10
}, function (err) {
$log.log('error in update to daybook', err);
});
}
$q.all(proms).then(function () {
defer.resolve('success');
});
return defer.promise;
}

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...