Hi I've reached a state with the following code where all is working except the timeout:
public monitorTask$(id: string): Observable<TaskResponse> {
return timer(0, 4000).pipe(
switchMap(() => this.fetchTaskStatus(taskId)),
timeout(120000),
takeWhile(res => this.isInProgress(res), true)
);
}
private postTask(id: string) {
this.monitorTask$(id).subscribe(
state => {
if (state.status === "SUCCESS") {
this.onSuccess();
}
if (state.status === "FAILURE) {
this.onFailure();
}
},
(err) => {
this.showError();
}
);
}
Also tried this:
public monitorTask$(id: string): Observable<TaskResponse> {
return interval(4000).pipe(
flatMap(() => this.fetchTaskStatus(id)),
takeWhile(res => this.isInProgress(res.status), true),
timeout(120000)
);
}
I'm expecting the timeout to error out and to enter the (err) block in postTask(), but it never reaches the timeout. I've been playing around with different variants, but don't seem to get it right.. This is the cleanest one I have, so if someone sees what I'm missing I would be really thankful!
There are multiple things to consider here.
timer
emit interval (4s) is less than the timeout
(120s). So the timeout
would never be triggered since the timer
emits every 4s.timeout
to RxJS build-in observable timer
makes little sense logically. I believe you wanted to pipe the timeout
to the this.fetchTaskStatus()
function.switchMap
, which would cancel the existing inner observable (this.fetchTaskStatus()
) when the outer observable (timer
) emits. Most probably you're looking for flatMap
operator. But beware for each emission of timer
, the this.fetchTaskStatus()
would be triggered individually.public monitorTask$(id: string): Observable<TaskResponse> {
return timer(0, 4000).pipe(
flatMap(() =>
this.fetchTaskStatus(id).pipe(
timeout(120000)
)
),
takeWhile(res => this.isInProgress(res.status), true),
);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments