Rxjs timeout() operator not working in pipe

Vluiz

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!

Michael D

There are multiple things to consider here.

  1. The timer emit interval (4s) is less than the timeout (120s). So the timeout would never be triggered since the timer emits every 4s.
  2. Piping a timeout to RxJS build-in observable timer makes little sense logically. I believe you wanted to pipe the timeout to the this.fetchTaskStatus() function.
  3. In that case there is one more issue. The mapping operator used here is 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.

edited at
0

Comments

0 comments
Login to comment

Related

Timeout Operator of RXJS not working as expected

pipe operator not behaving as expected RXJS

Angular replace pipe operator is not working

rxjs take operator - to limit results with async pipe

Multicast Operator with Pipe in RxJS 5.5+

Angular 2 + rxjs: async pipe with .share() operator

Using forkJoin as lettable operator in RxJS 6 pipe

RxJS pipe Finalize operator not getting called

RXJS throttletime operator is not working in Angular

next operator not working as expected on rxjs

rxjs 6 pipe is not working in Angular 6

Angular 6 - rxjs pipe not working on valueChanges

Reactive Development | RxJS async pipe not working

How to test timeout() in a rxjs pipe with jasmine-marbles

Pipe operator not working when combining with async pipe in template for valuechanges observable

Angular 6 pipe RxJs operator to chain 3 dependant observables

Rxjs share() operator with Behavior subject and async pipe - Angular

NgRx: Is the RxJS pipe operator necessary for store.select?

RxJs: Can you spread operators as arguments into pipe operator

RxJS take operator not working with Angular app

do rxjs operator is not working with ionic2

RxJs 6 DebounceTime operator not working properly

Importing a single rxjs operator not working with react and webpack

RxJs expand and delay operator not working as expected

Angular 4: Using HTTP interceptor + RxJS timeout operator error

Rxjs pipe not working with angular´s Http.get()

Rxjs map operator used after switchMap operator in a pipe can't find lift() function

Second pipe operator not working for my regular expression in python

Rxjs OR operator

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive