我对promises和for循环有问题。以下代码不是按顺序执行的:
secondQuery.find().then(function(results) {
alert("2")
for (var i in results) {
(function(index){
var object = results[index];
var item_id = results[index].id;
var status = object.get("status");
var event_id = object.get("event_id");
var user_id = object.get("user_id");
var user_id_name = object.get("user_id_name");
var thumbimageurl = "";
var nFacebookFriends = 0;
var nInstagramFollowers = 0;
var nTwitterFollowers = 0;
var query2 = new Parse.Query(User)
query2.equalTo("username",user_id)
return query2.first({
success: function(object) {
alert("3")
var thumbfile = object.get("thumb");
thumbimageurl = thumbfile.url();
nFacebookFriends = object.get('nFacebookFriends');
nInstagramFollowers = object.get('nInstagramFollowers');
nTwitterFollowers = object.get('nTwitterFollowers');
}
}).then(function(){
alert("4")
requests.push({
'thumb': thumbimageurl,
'item_id':item_id,
'status':status,
'event_id':event_id,
'user_id':user_id,
'user_id_name':user_id_name,
'nFacebookFriends':nFacebookFriends,
'nInstagramFollowers':nInstagramFollowers,
'nTwitterFollowers':nTwitterFollowers
})
})
})(i)
}
}).then(function(){
alert('end')
$scope.requests = requests;
$localStorage.requests = requests;
// alert(JSON.stringify(requests))
})
" 2"提醒而不是" 3"和" 4"我直接"结束"。有没有办法强制在下一个承诺之前执行for循环?
答案 0 :(得分:0)
您需要了解异步调用是如何发生的。这里发生的是最外面的块(secondQuery.find().then...
)被执行,并且当promise返回时,回调被同步执行(循环的所有迭代激发不同的异步调用),然后退出,以执行{ {1}}包含then
的块;因为在你的代码中,我发现alert('end')
是一个异步调用,在调用query2.first
块之前不能返回。
如果您希望在end
和end
后面有3
块,则必须将其链接到它们。这是保证您所需序列的唯一方法。或者您可以查找promise.all()(如果您的图书馆使用蓝鸟)或promise.all()(es6)
答案 1 :(得分:0)
您可以创建一个承诺链,其中每个承诺都在链中的先前承诺中待定。因此,在for循环的每次迭代中,您可以继续将promises
添加到promise chain
。
注意:但我有一个问题@ query2.first
这会返回一个承诺,还会回调吗?在这种情况下,我不知道回调部分会发生什么。
secondQuery.find().then(function(results) {
alert("2");
// We will create a chain of promises here,
// Each of it is pending on previouse promise in the chain
var promise = Promise.resolve();
for (var i in results) {
(function(index) {
promise = promise.then(function() {
var object = results[index];
var item_id = results[index].id;
var status = object.get("status");
var event_id = object.get("event_id");
var user_id = object.get("user_id");
var user_id_name = object.get("user_id_name");
var thumbimageurl = "";
var nFacebookFriends = 0;
var nInstagramFollowers = 0;
var nTwitterFollowers = 0;
var query2 = new Parse.Query(User);
query2.equalTo("username",user_id);
//I am not very sure about this, this return a promise also there is a callback
//So When callback hits is out of this scope
return query2.first({
success: function(object) {
alert("3")
var thumbfile = object.get("thumb");
thumbimageurl = thumbfile.url();
nFacebookFriends = object.get('nFacebookFriends');
nInstagramFollowers = object.get('nInstagramFollowers');
nTwitterFollowers = object.get('nTwitterFollowers');
}
})
})
.then(function() {
alert("4");
requests.push({
'thumb': thumbimageurl,
'item_id':item_id,
'status':status,
'event_id':event_id,
'user_id':user_id,
'user_id_name':user_id_name,
'nFacebookFriends':nFacebookFriends,
'nInstagramFollowers':nInstagramFollowers,
'nTwitterFollowers':nTwitterFollowers
})
})
})(i)
}
return promise;
})
.then(function(){
alert('end');
$scope.requests = requests;
$localStorage.requests = requests;
// alert(JSON.stringify(requests))
})