我正在使用循环将数据更新到服务器,一旦收到服务器的响应,它就会在我的本地db.But中更新,然后在本地db中完成所有更新之前,promise将得到解决并且只有一个数据会更新。任何人都可以告诉我为什么在完成for循环中的所有过程之前,promise会得到解决?
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;
}
答案 0 :(得分:1)
首先,您可以在此行之前添加 self.password_entry = Entry(
self.frame, bg="white", bd=2,
font=self.PASSWORD_ENTRY_FONT, relief=FLAT, highlightthickness=1, highlightbackground="black",
selectbackground="yellow", show='*', highlightcolor='#4584F1')
self.password_entry.place(
relx=float(self.PASSWORD_ENTRY_X) / self.VIRTUAL_WIDTH,
rely=float(self.PASSWORD_ENTRY_Y) / self.VIRTUAL_HEIGHT,
relheight=float(60) / 768, relwidth=float(454)/1366)
self.password_entry.bind('<FocusIn>', self.password_entry_handler)
self.password_entry.bind('<FocusOut>', self.password_entry_handler)
self.password_entry.bind("<Tab>", no_op)
:console.log(proms.length);
吗?
我猜它会打印0,因为第一次向$q.all(proms).then(function () {
数组推送任何内容都会异步发生,这意味着在解释器完成运行proms
函数之后。
如果情况确实如此,请考虑将其更改为类似的内容:
updatecategories
然后,当您点击proms.push(save(data[i]).then(function (result) {
$log.log('values changed in daybook', result);
}, function (err) {
$log.log('error in update to daybook', err);
}));
行时,$q.all(proms)
将是一个充满未解决的承诺的数组,其中proms
将等待所有要解决的承诺,然后是{{ 1}}也会解决。