Wie zu warten, für Abonnements innerhalb einer for-Schleife abgeschlossen ist, bevor Sie fortfahren

Ich habe ein Abonnement innerhalb einer for-Schleife holt JSON-Daten als eine Reihe von "Kategorie " Daten" von einer externen Quelle, dann Filter, die Daten basierend auf die aktuelle Lage. Was ich brauche, ist zu warten, bis Sie alle Abonnements abgeschlossen ist, und dann, und nur dann kann meine app gehen. Gerade jetzt, es wartet nicht, bis Sie alle Abonnements abgeschlossen, es wird nur abgeschlossen, ein paar, und geht dann, während die anderen Abonnements weiter in den hintergrund.

Ich habe versucht, die folgenden "brute force" - Methode, wo ich weiß, eine bestimmte Anzahl von Kategorien werden Hinzugefügt, um das gefilterte array, und es funktioniert, aber ich weiß nicht, wie es funktioniert für jede situation.

Hier ist mein code:

getMultipleCategoryData(categoryIds: string[]) {
for (let i = 0; i < this.waypointIds.length; i++) {
//after user selects their categories, its added to waypointNames, and occurences() gets the # of occurences of each waypoint name
  let occurences = this.occurences(this.waypointNames[i], this.waypointNames);
  this.categoryApi.getCategoryData(this.waypointIds[i]).toPromise().then(data => {

    let filteredLocs = data.locations.filter(loc => this.distanceTo(loc, this.hbLoc) < MAX_RADIUS);
    let foundLocs = [];
    //fill filteredLocs with first n, n = occurences, entries of data 
    for (let n = 0; n < occurences; n++) {
      if (filteredLocs[n] != undefined) {
        foundLocs[n] = filteredLocs[n];
      }
    }
    //find locations closest to hbLoc (users current location), and add them to waypoints array
    for (let j = 0; j < foundLocs.length; j++) {
      for (let k = 0; k < filteredLocs.length; k++) {
        if (this.distanceTo(this.hbLoc, filteredLocs[k]) < this.distanceTo(this.hbLoc, foundLocs[j]) && foundLocs.indexOf(filteredLocs[k]) < 0) {
          foundLocs[j] = filteredLocs[k];
        }
      }
    }
    if (foundLocs.length > 0 && foundLocs.indexOf(undefined) < 0) {
      for (let m = 0; m < foundLocs.length; m++) {
        this.waypointLocs.push(foundLocs[m]);
      }
    }
  }).then(() => { 
    //this hardcoded, brute force method works, but i'd need it to be more elegant and dynamic
    if (this.waypointLocs.length >= 5) {
      let params = { waypointLocs: this.waypointLocs, hbLoc: this.hbLoc };
      this.navCtrl.push(MapPage, params);
    }
  });
}
}

Und die categoryApi.getCategoryData Methode:

getCategoryData(categoryId): Observable<any> {
    //don't have data yet
    return this.http.get(`${this.baseUrl}/category-data/${categoryId}.json`)
        .map(response => {
            this.categoryData[categoryId] = response.json();
            this.currentCategory = this.categoryData[categoryId];
            return this.currentCategory;
        });
}

Alles ist in Ordnung, außer zu warten, für die Abonnements abschließen, ich möchte wirklich einen Weg, um zu bestimmen, wenn Sie alle Abonnements abgeschlossen haben. Jede Hilfe ist willkommen!

InformationsquelleAutor AlexT | 2017-06-26
Schreibe einen Kommentar