亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

等待訂閱在 while 循環中完成其工作(打字稿)

等待訂閱在 while 循環中完成其工作(打字稿)

溫溫醬 2023-03-24 16:00:03
我有一個 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 本身,它就會無限運行
查看完整描述

1 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

使用遞歸函數代替循環。


this.counter = 0;

this.max = 3;


function subscribe() {

  

  this.ms.getPopular(this.tv, this.counter).subscribe(val => {

    this.afstore.collection("matches").doc(this.id).valueChanges().pipe(take(1)).subscribe((val2: any) => {


      if (this.counter == this.max) {

        return;

      } else {

        this.counter++;

        this.subscribe();

      }


    });

  });

}


查看完整回答
反對 回復 2023-03-24
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號