jascript angular for循环在promise之后执行
jascript angular for loop executed after the promises
我对promise和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"answers"4"的循环之后,我直接得到了"结束"。有没有办法强制在下一个promise之前执行for循环?
您需要了解异步调用是如何发生的。这里发生的是,最外层的块(secondQuery.find().then...
)被执行,当promise返回时,回调被同步执行(循环的所有迭代激发不同的异步调用),然后退出,以执行包含alert('end')
的then
块;因为在您的代码中,我观察到query2.first
是一个异步调用,它在调用end
块之前不能返回。
如果您希望end
块位于3
和4
之后,则必须将其链接到它们。这是保证你想要的顺序的唯一方法。或者,您可以查找promise.all()(如果您的库使用bluebird)或promise.al()(es6)
您可以创建一个承诺链,每个承诺都挂在链中的前一个承诺上。所以在for循环的每次迭代中,您可以不断地将promises
添加到promise chain
中。
注意:但我有一个问题@query2.first
,它返回了一个promise,还回调了?在这种情况下,我不知道回调部分会发生什么。
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))
})
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 使用promise和mongoose对文档进行排序
- 在chrome.tabs.onCreated之后加载HTML页面
- 在Jquery detachment()和appendTo()之后定位元素
- AngularJS:ng之后,重复$scope值未按预期更新
- Promise函数在.then之后未运行函数化
- 在向Promise添加监听器之后,我应该使用原始的Promise还是新的Promise
- 为什么在 Promise.all() 之后没有调用 onJect,其中数组中包含的 Promise.reject() 传
- React不会在promise中使用的setState之后重新应答
- 在promise之后或在promise内部返回值
- 为什么defered.promise在defered.resolve(myobject)之后的结果与myobject不同
- 在检索到promise之后定义then()
- 为什么不't运行此promise调用之后的代码
- 使用循环在另一个函数之后运行一个函数&jQuery/JavaScript中的promise
- 在if语句之后执行promise,比如finally
- jQuery在.promise()之后循环一个函数
- AngularJS指令:如何在promise之后编译html
- AngularJS:在promise之后的指令中失去作用域
- jascript angular for循环在promise之后执行
- AngularJS: promises,你能在使用.then()之后传递一个promise吗?