我有一個 while 循環,在 while 循環內我有兩個訂閱。我怎樣才能等他們完成才能繼續使用 while 循環?this.counter = 1;array[0] = 3;while(this.counter <= array[0]) { console.log("WHILE: "+this.counter); this.ms.getPopular(this.tv, this.counter).subscribe(val => { this.afstore.collection("matches").doc(this.id).valueChanges().pipe(take(1)).subscribe((val2: any) => { console.log(this.counter); if(this.counter == array[0]) { return; } //console.log("YES"); this.counter++; }); }); this.counter++; }我目前的輸出是WHILE: 1WHILE: 2WHILE: 3456但我想要得到的輸出是WHILE: 1123this.counter++在 while 循環中甚至不需要。如果沒有 while 本身,它就會無限運行
等待訂閱在 while 循環中完成其工作(打字稿)
溫溫醬
2023-03-24 16:00:03