forループの非同期呼び出しに問題があります。ループは、非同期呼び出しが終了する前に継続します。私はこの言語にはかなり新しく、コールバックなどを把握しようとしています。私は自己呼び出し関数、約束、タイムアウトを試みましたが、意図したとおりに動作するようにフローを取得することはできません。Forループの非同期呼び出しのシーケンス - Javascript
プロファイルオブジェクトがメッセージ配列にプッシュされる前に、firebaseへの呼び出しを完了します。
// 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);
}
お気軽にお問い合わせください。あなたが実際には2つの別々のものにしたいよう
おかげで、 ノエル
あなたは 'chat.messages.push(プロファイル)を含めてみまし;' [値]イベントハンドラ内で? – guest271314
スポットがあります。それはそれを修正した。見て、応答する時間を取ってくれてありがとう:-)シンプルだけど、何時間も頭を叩いていました! –