简体   繁体   English

嵌套订阅无限循环-Angular / RxJS

[英]Nested subscribe infinity loop - Angular / RxJS

I'm doing a nested subscribe that I know it's not the perfect way. 我正在做一个嵌套订阅,我知道这不是完美的方法。 I have something like this : 我有这样的事情:

 this.route.params.subscribe((params) => {            
        this.order$
            .getMailCommande(this.id)
            .subscribe((datas) => {
                var Object = localStorage.getItem('dataCommand');
                var test = JSON.parse(retrievedObject);
                this.over = test.over;
                while (this.over == false) {
                    this.order$
                        .getMoreInfo(this.id, this.type, this.nbr)
                        .subscribe((datas2) => {
                            var array = datas2.MoreInfo;
                            var Object2 = localStorage.getItem('dataCommand');
                            var test2 = JSON.parse(Object2);
                            test2.ListeInfo.push(array);
                            this.over = true;
                        });
                }

Since I added the loop I got an infinity loop, i'm looking for changing this code with the switchmap or operators from RxJS but I haven't yet succeeded. 自从我添加了循环后,我得到了一个无限循环,我正在寻找使用RxJS的switchmap或运算符更改此代码的方法,但尚未成功。

Any ideas of my infinity loop and how to keep the same logic ? 关于无限循环的任何想法以及如何保持相同的逻辑?

Thanks in advance 提前致谢

If you want to do the required logic in a loop and break when some condition matches then how about having a separate method which calls itself unless the condition is satisfied?? 如果要在循环中执行所需的逻辑并在某些条件匹配时中断,那么除非满足条件,否则要有一个单独的方法进行自我调用?

this.route.params.subscribe((params) => {            
        this.order$
            .getMailCommande(this.id)
            .subscribe((datas) => {
                var Object = localStorage.getItem('dataCommand');
                var test = JSON.parse(retrievedObject);
                this.over = test.over;
                while (this.over == false) {
                    this._loopMeOver.subscribe();
                }
            })
})


private _loopMeOver() {

    return this.order$
        .getMoreInfo(this.id, this.type, this.nbr)
        .map((datas2) => {
            var array = datas2.MoreInfo;
            var Object2 = localStorage.getItem('dataCommand');
            var test2 = JSON.parse(Object2);
            test2.ListeInfo.push(array);
            if (condition is successfull) {
                //return true mayBe
            }
            else {
                this._loopMeOver().subscribe()
            }
        });
}

I can't see your full code to get the whole idea of what you are trying to do. 我看不到完整的代码,无法完全了解您要执行的操作。 But I kinda feel that you just started learning rxjs and the this.over thing is trying to keep checking if your nested request is finished or not. 但是我有点觉得您刚刚开始学习rxjs, this.over事情试图保持检查嵌套请求是否完成。 And there are various mistakes in your example code btw. 而且示例代码btw中存在各种错误。

So yes you would need other rxjs operators like switchMap() to make your code cleaner. 因此,是的,您将需要其他rxjs运算符(如switchMap()来使代码更switchMap() I'll use the new rxjs pipable syntax to show you a general idea of how to do that. 我将使用新的rxjs pipable语法向您展示如何执行此操作的一般思路。

this.route.params.pipe(
  // this switchMap() is called when route params changes
  // order$ seems not a Observable, it's misleading to name it with an ending $
  switchMap(() => this.order$.getMailCommande(this.id)),
  // this tap() operator is called after getMailCommande() finish
  tap(() => {
    var retrievedObject = localStorage.getItem('dataCommand');
    var test = JSON.parse(retrievedObject);
    this.over = test.over;
  }),
  // this switchMap() is called after code inside tap is called
  switchMap(() => this.order$.getMoreInfo(this.id, this.type, this.nbr)),
  // this tap() is called after getMoreInfo() finish
  tap(datas2 => {
    var array = datas2.MoreInfo;
    var Object2 = localStorage.getItem('dataCommand');
    var test2 = JSON.parse(Object2);
    test2.ListeInfo.push(array);
    this.over = true;
  })
).subscribe();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM