Why switchMap does not cancel previous observer?

做~自己de王妃 提交于 2019-12-01 13:12:16

Because every time you call requestPassportData() you'll create a new chain with new Observable.timer that has nothing to do with the previous calls.

There are obviously multiple ways to solve this but you can for example do the following:

private requestPasswordSubject$ = new Subject();

private requestPassword = this.requestPasswordSubject$
  .switchMap(() => Observable.timer(0, 1000)
    .switchMap(() => this.requestMethods.requestPassportData(data))
    .takeWhile(() => {
      return (
         th.formRegister.currentForm().index == 1 ||
         th.formRegister.currentForm().index == 2 ||
         th.formRegister.currentForm().index == 3
      );
    });
  )
  .subscribe(response => {});

public requestPassportData(): void {
  ...
  this.requestPasswordSubject$.next();
}

Actual implementation depends on what exactly you're trying to achieve but hopefully you get the point what is different to what you have now.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!