Home > Net >  Rxjs only execute last item in queue with concatMap
Rxjs only execute last item in queue with concatMap

Time:04-29

I am using NGRX with RXJS. In the effects layer, I am using a concatMap to queue my requests, however, once the latest request is complete, I want to execute the last item added to the queue instead of all the remaining ones. Is this even possible ?

I have tried using mergeMap, switchMap, etc, but I need to run the requests synchronously instead of concurrently. Which is why I need to use concatMap (or something similar).

    updateThing$ = createEffect(() =>
    this.actions$.pipe(
        ofType(updateThingRequest),
        concatMap((action) => {

            return this.myService
            //Once this request is complete, Instead of executing all of the pending requests from the concatMap queue,
            //I only want to execute the last request pending in the concatMap queue, if there are any, and remove all the other pending ones.
                .update(action)
                .pipe(
                    map(() => {

                        return someActionComplete();
                    }),
                    catchError((err) => {
                        return of(
                            someActionError()
                        );
                    })
                );
        })
    )
);

CodePudding user response:

You can use exhoustMap it will wait untli inner observable completes and ingore other values that will come.

https://www.learnrxjs.io/learn-rxjs/operators/transformation/exhaustmap

CodePudding user response:

I'm not sure if it's correct solution but maybe you can use finalize operator? (or try to figure out proper operator by rxjs decision tree)

  • Related