我在for循环中有一个订阅,它从外部源获取JSON数据作为一系列"类别数据",然后根据用户当前位置过滤该数据.我需要的是等待所有订阅完成,然后才能继续我的应用程序.现在,它不等待所有订阅完成,它只完成一些订阅,然后继续,而其他订阅在后台继续.
我已经尝试了以下"强力"方法,我知道一定数量的类别将被添加到已过滤的数组中,并且它可以工作,但我不知道如何使其适用于任何情况.
这是我的代码:
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] …Run Code Online (Sandbox Code Playgroud)