[英]Ember js controllerSetup with call to async model works with breakpoint but not without
[英]Ember JS: Sequential dependent async call to process
将请求提交到服务器时遇到问题。 在获取其他属性以处理它们之后,我需要发送数据。 用户可以是新用户,也可以是现有用户(带有/不带有公司名称)。
当我打电话时,它的顺序不正确,并且最终对象没有适当的数据,并且由于异步处理的时序而获取了随机数据。 For循环将尽早完成。
for (var i = 0; i<_uniqEditUsersList.length; i++){
var _uniqEditUser = _uniqEditUsersList[i];
var newUserid = $("[id='" + _uniqEditUser + "']")[0].value;
console.log(_uniqEditUser + ' Data '+ newUserid);
Service.searchUser(newUserid).then(function (userResult) {
console.log("userResult: " + _.keys(userResult).length);
console.log(_uniqEditUser2 + ' Data '+ newUserid2);
var currentCartId = self.get('cart').id;
var userResult = userResult ? userResult : "";
if (_.keys(userResult).length == 0) {
CompanyName = "NEW_USER";
var newData = {oldId: _uniqEditUser, newid: newUserid, CompanyName: CompanyName};
dataMap[counter] = newData;
if(counter == _uniqEditUsersList.length){
self.set('processList', dataMap);
self.processListFunction();
}
counter = counter + 1;
} else {
Service.getUsers(partnerListObj).then(function (result) {
if (result.users.totalResults == "1") {
CompanyName = result.company;
} else {
CompanyName = "NO_COMPANY"
}
var newData = {oldId: _uniqEditUser, newid: newUserid, CompanyName: CompanyName};
dataMap[counter] = newData;
if(counter == _uniqEditUsersList.length){
self.set('processList', dataMap);
self.processListFunction();
}
counter = counter + 1;
}, function (err) {
self.showErrorMessage(JSON.parse(err.responseText).message);
});
}
}, function (err) {
self.showErrorMessage(JSON.parse(err.responseText).message);
});
}
如何使调用相互依赖,并使for循环顺序处理?
我尝试了很多事情,但没有为我工作。
也许以下内容可能对您有用。 当然要进行一些调整。 使用reduce每个承诺将取决于先前的承诺。
arrayOfTasksToResolveInOrder.reduce((promise, task) => {
return promise.then((result) => {
if (result.condition) {
return task.fetch(result));
} else {
return task.someOtherFetch(result);
}
});
}, Ember.RSVP.Promise.resolve());
这篇文章的作者在解释此模式方面做得很好, Promises Part II-Advanced promise模式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.