2017-10-24 3 views
0

ionic으로 처음으로 캐싱을 설정합니다. 여기에 코드입니다 :약속이 끝나기 전에 실행되는 Promise.all의() 함수 - Ionic/Angular

listProducts(): Promise <any> { 
    let cacheKey = 'products'; 
    this.cache.removeItem(cacheKey); 
    let promises_array:Array<any> = []; 
    let results; 
    return new Promise((resolve, reject) => { 
     this.cache.getItem(cacheKey).catch(() => { 
      this.list = this.af.list('/products'); 
      this.subscription5 = this.list.subscribe(items => { 
       for (let x = 0; x < items.length; x++) { 
        promises_array.push(new Promise((resolve, reject) => { 
         console.log(JSON.stringify(items[x].customMetadata) + ": this is the customdata (((()()()()()"); 
         let storageRef = firebase.storage().ref().child('/settings/' + items[x].customMetadata.username + '/profilepicture.png'); 
         storageRef.getDownloadURL().then(url => { 
          console.log(url + "in download url !!!!!!!!!!!!!!!!!!!!!!!!"); 
          items[x].customMetadata.profilepic = url; 
         }).catch((e) => { 
          console.log("in caught url !!!!!!!$$$$$$$!!"); 
          items[x].customMetadata.profilepic = 'assets/blankprof.png'; 
         }); 
         //this.startAtKey = item.$key; 
         this.productListArray.push(items[x].customMetadata); 
        })); 
       }; 
      }) 
      results = Promise.all(promises_array); 
      results.then(() => { 
       //setTimeout(() => { 
       console.log(JSON.stringify(this.productListArray) + " value value vlaue productlistarray"); 
       this.productListArray.reverse(); 
       return this.cache.saveItem(cacheKey, this.productListArray); 
       //}, 3000); 
      }) 
     }).then(data => { 
      console.log("Saved data: ", data); 
      resolve(); 
     }) 
    }) 
} 

기본적으로 라인 console.log(JSON.stringify(this.productListArray) + " value value vlaue productlistarray");는 하늘의 배열을 인쇄 - []. 모든 약속이 완료되기 전에 코드가 실행되어서는 안되기 때문에이 변수에 데이터가 있어야합니다. 당신은 내가 setTimeout을 주석 처리했음을 알 수 있습니다 - 주석으로 달았을 때, 그것은 작동합니다 -하지만 이것은 분명히 받아 들일 수있는 해결책이 아닙니다, 나는 단지 약속에 타이밍 문제가 있다는 것을 보여줄뿐입니다. 어떤 도움이라도 좋을 것입니다.

답변

0

나는 문제가

console.log(JSON.stringify(this.productListArray) + " value... 

이 실행될 때 기본적으로 목록은 여전히 ​​가져 오는되는이 라인

this.list.subscribe(items => { 

에있다 생각합니다. 그래서 나는 promises_array이 비어 있다고 생각합니다 (당신은 체크 아웃 할 수 있습니다).

당신은 매핑 list하여 해당을 정렬 할 수 있습니다 개별 약속을 밀어 대신 약속의 배열로 (지도 중() 또는 flatMap(), 어떤 확인해야합니다).

을 나는 (이지도 생각 또는 flatMap) 코드를 사용하지만 테스트가 필요합니다. 들여 쓰기 수준마다 많은 반환이 필요합니다.

const promises_array = this.list.map(items => { 
    return items.map(item => { 
    return new Promise((resolve, reject) => { 
     console.log(JSON.stringify(items[x].customMetadata) + ": this is the customdata (((()()()()()"); 
     let storageRef = firebase.storage().ref().child('/settings/' + items[x].customMetadata.username + '/profilepicture.png'); 
     return storageRef.getDownloadURL().then(url => { 
     console.log(url + "in download url !!!!!!!!!!!!!!!!!!!!!!!!"); 
     items[x].customMetadata.profilepic = url; 
     }).catch((e) => { 
     console.log("in caught url !!!!!!!$$$$$$$!!"); 
     items[x].customMetadata.profilepic = 'assets/blankprof.png'; 
     }); 
     this.productListArray.push(items[x].customMetadata); 
    }));  
    }) 
} 
Promise.all(promises_array) 
    .then(() => { 
    console.log(JSON.stringify(this.productListArray) + " value value vlaue productlistarray"); 
    this.productListArray.reverse(); 
    this.cache.saveItem(cacheKey, this.productListArray); 
}) 
+0

내 응용 프로그램에서 실제로이 기술을 사용했습니다 ... 아프지 마십시오. 정말 잘 모르겠지만 요. – ewizard