对循环的异步调用进行排序-Javascript
我在for循环中遇到异步调用问题。在异步调用完成之前,循环将继续。我对这门语言还很陌生,试图掌握回调……等等。我已经尝试了一个自调用函数,承诺和超时,但仍然无法让流按预期工作 我希望在profile对象被推入messages数组之前完成对firebase的调用对循环的异步调用进行排序-Javascript,javascript,angularjs,ionic-framework,firebase,firebase-realtime-database,Javascript,Angularjs,Ionic Framework,Firebase,Firebase Realtime Database,我在for循环中遇到异步调用问题。在异步调用完成之前,循环将继续。我对这门语言还很陌生,试图掌握回调……等等。我已经尝试了一个自调用函数,承诺和超时,但仍然无法让流按预期工作 我希望在profile对象被推入messages数组之前完成对firebase的调用 // returns the chats for that profile Chat.allChatsByUser(uid).$loaded() .then(function(data) { for (var i = 0
// returns the chats for that profile
Chat.allChatsByUser(uid).$loaded()
.then(function(data) {
for (var i = 0; i < data.length; i++) {
// self calling function for async callback handling
// this ensures that the async call is run for every iteration in the loop
(function(i) {
var item = data[i];
// function to arrange users and chats fom newest to oldest
// for each matched user. item.$id = uid
Auth.getProfile(item.$id).$loaded()
.then(function(profile) {
// first function handles success
if (typeof profile === 'object') {
if(chat.keyOrder == 'true') {
// get last chat from firebase
// WANT THIS COMPLETE BEFORE CONTINUING
ref.child('chatting').child('messages').child(chat.chatId).on("value", function(data) {
profile.lastChat = data.child('lastMsg').val();
});
// pushes chatting users profile into the array
chat.messages.push(profile);
} else {
// invalid response
return $q.reject(profile);
}
}, function(profile) {
// promise rejected
console.log('error', error);});
// i argument as closure
})(i);
}
//返回该配置文件的聊天记录
Chat.allChatsByUser(uid)。$loaded()
.then(函数(数据){
对于(变量i=0;i
感谢任何帮助或指导
谢谢,
Noel所以听起来你想要两件不同的事情实际上,一件,你想要你的循环在异步调用完成之前不继续。我很确定es6有很多奇妙的方法可以做到这一点,但是你没有使用es6。我不确定你为什么想要循环等待?所以我临时用了一个while循环。第二,你想要什么“希望在配置文件对象被推送到消息数组之前完成对firebase的调用。”如第一条注释所述,这是通过将推调用放入
值
事件处理程序来完成的
// returns the chats for that profile
Chat.allChatsByUser(uid).$loaded()
.then(function(data) {
var i = 0;
var inProgress = false;
while (i < data.length) {
// self calling function for async callback handling
// this ensures that the async call is run for every iteration in the loop
// wait until last iteration has completed
while(inProgress);
// the function is about to begin
inProgess = true;
(function(i) {
// increment i here
var item = data[i++];
// function to arrange users and chats fom newest to oldest
// for each matched user. item.$id = uid
Auth.getProfile(item.$id).$loaded()
.then(function(profile) {
// first function handles success
if (typeof profile === 'object') {
if (chat.keyOrder == 'true') {
// get last chat from firebase
// WANT THIS COMPLETE BEFORE CONTINUING
ref.child('chatting').child('messages').child(chat.chatId).on("value", function(data) {
profile.lastChat = data.child('lastMsg').val();
// wait until event
// pushes chatting users profile into the array
chat.messages.push(profile);
// allow next iteration to continue
inProgess = false;
});
} else {
// invalid response
return $q.reject(profile);
}
},
function(profile) {
// promise rejected END script
return console.log('error', error);
});
// i argument as closure
})(i);
}
//返回该配置文件的聊天记录
Chat.allChatsByUser(uid)。$loaded()
.then(功能(数据){
var i=0;
var inProgress=假;
而(i
在值处理程序中包含聊天.消息.推送(配置文件)
ref.child('chatting').child('messages').child(chat.chatId)
.on("value", function(data) {
profile.lastChat = data.child('lastMsg').val();
// pushes chatting users profile into the array
chat.messages.push(profile);
});
我认为.all方法就是这样的方法。例如:
function loadMeetings(city,state) {
return ref.child('states').child(state).child(city).once('value').then(function(snapshot) {
var reads = [];
snapshot.forEach(function(childSnapshot) {
var id = childSnapshot.key();
var promise = ref.child('meetings').child(id).once('value').then(function(snap) {
return snap.val();
}, function(error) {
// The Promise was rejected.
console.error(error);
});
reads.push(promise);
});
return Promise.all(reads);
}, function(error) {
// The Promise was rejected.
console.error(error);
}).then(function(values) {
//for each snapshot do something
});
}
推送到值事件处理程序中的数组:
ref.child('chatting').child('messages').child(chat.chatId)
.on("value", function(data) {
profile.lastChat = data.child('lastMsg').val();
// pushes chatting users profile into the array
chat.messages.push(profile);
});
您是否尝试过在value
event handler中包含chat.messages.push(profile);
事件处理程序?现在就解决了。感谢您花时间查看和回复:-)太简单了,但是我的头一直晃了好几个小时。嗨,丹尼尔。谢谢你的回应。我会在执行类似的数据流时使用这个格式。试着从顺序编码的背景中习惯使用异步编程。把它放在值事件处理程序中。非常感谢。没问题,请考虑选择答案。