약속 루프를 사용하는 방법, 내 문제가 해결 호출하는 곳과 관련이있다 생각하지만 난 잘 모르겠어요내가 더 성공을 약속 내에서 루프를 실행하기 위해 노력하고있어
/*
* Get conversations of user
* @param user {String}
*/
function getConversations(user){
\t return new Promise(function(resolve, reject){
\t \t var conversations = user.Conversations
\t \t var newConversations = []
\t \t for(var conversation of conversations) {
\t \t \t helperGetConvo(conversation.ConversID).then(function(convo){
\t \t \t \t newConversations.push(createConversationObject({messages:[], name:convo.conversationName, users:["broulaye", "doumbia"], Id:convo.conversationID}))
\t \t \t }).catch(function(reason) {
\t \t \t \t console.log("failure when finding conversation 2: " + reason)
\t \t \t })
\t \t }
\t \t resolve(newConversations)
\t })
}
function helperGetConvo(convoId) {
\t return new Promise (function(resolve, reject){
\t \t query.findConversation(convoId).then(function(convers) {
\t \t \t if(convers) {
\t \t \t \t console.log("conversation was found: " + convers)
\t \t \t }
\t \t \t else {
\t \t \t \t console.log("conversation was not found: " + convers)
\t \t \t }
\t \t \t resolve(convers)
\t \t }).catch(function(reason) {
\t \t \t console.log("failure when finding conversation: " + reason)
\t \t })
\t })
}
이와 같은 코드를 실행할 때 getConversations 함수는 빈 배열 만 반환합니다. 내가 getConversations을 변경할 때하지만 지금 같은 기능 :
function getConversations(user){
\t return new Promise(function(resolve, reject){
\t \t var conversations = user.Conversations
\t \t var newConversations = []
\t \t for(var conversation of conversations) {
\t \t \t helperGetConvo(conversation.ConversID).then(function(convo){
\t \t \t \t newConversations.push(createConversationObject({messages:[], name:convo.conversationName, users:["broulaye", "doumbia"], Id:convo.conversationID}))
\t \t \t \t resolve(newConversations)
\t \t \t }).catch(function(reason) {
\t \t \t \t console.log("failure when finding conversation 2: " + reason)
\t \t \t })
\t \t }
\t })
}
내가 그러나 그것은 내가 믿는 전체 forloop 통과하지 않는 출력을 얻을 때문에 return 문처럼 내 이해 해결 작업에서 .
누군가의 도움이 PLZ
약속이 해결 될 때까지 기다려야합니다. Promise.all을 살펴보십시오. 나는 또한 약속에 대한 더 많은 독서를 조언 할 것이고, 당신의 코드는 정말로 엉망이다. –
[약속 생성자 방지 패턴] 방지 (https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) –