M
There's a few operators for this, you're using it wrong. takeUntil♪ takeUntil - Just one of the operators who's signed for some flow and listens. next It's a big deal. timer(60000), but in your problem, I can see that you need to get off as soon as you can. response = 1:import { Subject } from 'rxjs/Subject';
import { timer } from 'rxjs/observable/timer';
import { switchMapTo } from 'rxjs/operators/switchMapTo';
import { takeUntil } from 'rxjs/operators/takeUntil';
const destroy$ = new Subject<void>();
timer(3000, 3000).pipe(
switchMapTo(this.requestMethods.requestPassportData(data)),
takeUntil(destroy$)
).subscribe((response) => {
if (response !== 1) {
return;
}
destroy$.next();
destroy$.complete();
});
Note that takeUntil We need to use it after. switchMapNot before, because switchMap creates a new flow, you'. flatMap - he's already outdated for over a year. rxjs >= 5.5♪Second option using takeWhile:import { timer } from 'rxjs/observable/timer';
import { switchMapTo } from 'rxjs/operators/switchMapTo';
import { takeWhile } from 'rxjs/operators/takeWhile';
let shouldTake = true;
timer(3000, 3000).pipe(
switchMapTo(this.requestMethods.requestPassportData(data)),
takeWhile(() => shouldTake)
).subscribe((response) => {
if (response !== 1) {
return;
}
shouldTake = false;
});
takeWhile He has his underwater rocks - he doesn't cover the flow, different from the takeUntil♪Third and most elegant using filter and take:import { timer } from 'rxjs/observable/timer';
import { switchMapTo } from 'rxjs/operators/switchMapTo';
import { take } from 'rxjs/operators/take';
import { filter } from 'rxjs/operators/filter';
timer(3000, 3000).pipe(
switchMapTo(this.requestMethods.requestPassportData(data)),
filter((response) => response === 1),
take(1)
).subscribe((response) => {
console.log(response)
});
take The argument accepts the number of events that need to be seduced, and the operator itself completes the flow and shuts down, i.e., there is no need to. Subscription to the variable and to write somewhere unsubscribe♪ take(n) - I'll do it for you.