Sunday 6 January 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...