for 루프에서 비동기 호출에 문제가 있습니다. 비동기 호출이 완료되기 전에 루프가 계속됩니다. 나는이 언어에 익숙하지 않고 콜백을 파악하려고 노력하고있다. 자체 호출 함수, 약속 및 제한 시간을 시도했지만 여전히 의도 한대로 작동하는 흐름을 가져올 수 없습니다.For 루프에서 비동기 호출 시퀀스 - 자바 스크립트
프로파일 객체가 메시지 배열로 푸시되기 전에 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);
}
감사합니다. 실제로 두 개의 별도 일을하려는 것처럼
덕분에, 노엘
당신이'chat.messages.push (프로파일)을 포함 해봤;''값 '이벤트 핸들러 내에서? – guest271314
Spot on. 그것은 그것을 고쳤다. 보고 반응하는 시간을내어 주셔서 감사합니다 :-) 너무 단순하지만 몇 시간 동안 내 머리를 두 드렸습니다! –