Performing a series of HTTP requests within a single @ngrx/effect

I need some guidance as I am new to @ngrx and feeling a bit lost in understanding how it should be used.

Let's assume we have an effect named PlaceOrderEffect

In this effect, my goal is to handle each request in a specific order.

processOrder$ = createEffect(
   ofType(OrderActions.PROCESS_ORDER),
   //this part is where I'm struggling
   concatMap([
      this.service.addCrust(),
      this.service.addTopings(),
      this.service.sendToCook()
      this.service.checkOut()
   ]
)

How can I ensure that these operations run sequentially and manage the final response effectively?

Answer №1

concatMap is a useful RxJS operator when used with the pipe. It merges the source observable with a new one returned by concatMap, but in your specific case, it may not be necessary.

If your requests are independent of each other, you can achieve the desired outcome by using the RxJS concat function as shown below:

processOrder$ = createEffect(() =>
    this.actions$.pipe(
        ofType(OrderActions.PROCESS_ORDER),
        switchMap(({ payload }) =>
            concat(
                this.service.addCrust(),
                this.service.addToppings(),
                this.service.sendToCook(),
                this.service.checkOut()
            )
        )
    )
);

However, if each request depends on the previous one, you should use the concatMap operator like this:

processOrder$ = createEffect(() =>
    this.actions$.pipe(
        ofType(OrderActions.PROCESS_ORDER),
        concatMap(({ payload }) => this.service.addCrust(payload)),
        concatMap((response1) => this.service.addToppings(response1)),
        concatMap((response2) => this.service.sendToCook(response2)),
        concatMap((response3) => this.service.checkOut(response3))
    )
);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Change a nullable string property within an interface to a non-nullable string property

Looking at two interfaces, one with a nullable vin and the other without: interface IVehicle { vin: string | null; model: string; } interface IVehicleNonNullVin { vin: string; model: string; } The goal is to convert a model from IVehicle ...

What are the potential drawbacks of using this alternative method instead of `useCallback` in React to prevent unnecessary re-renders of children components?

While exploring some code, I stumbled upon an alternative method to prevent function recreation during renders compared to the official useCallback hook. This method seems to offer certain benefits over useCallback, but I am interested to know if there are ...

Guidelines for Organizing Angular Interface Files and Implementing Custom Type Guards

In my Angular 2 project, I am utilizing Interfaces and have implemented User Defined Type Guards: grid-metadata.ts export interface GridMetadata { activity: string; createdAt: object; totalReps: number; updatedAt: object; } grid.service.ts ... ...

Is it possible for an object to receive notifications when a component object undergoes changes in Angular 2/4?

When working with Angular components, it's possible to pass a variable or object as @Input(). The component will be notified whenever the value of this input changes, which is pretty cool... I'm currently developing an application that features ...

Assign variable data to properties within an immutable object within a React component

I have declared a Const in my config.service.ts file like this: export const mysettings={ userid:"12324", conf:{ sessionDuration:30, mac:"LON124" } } I am using this constant in various components. However, instead of hardcoding these val ...

What's causing the malfunction of Angular subscriptions when switching between tabs?

I have developed a basic angular application that necessitates users to authenticate for WebEx in order to obtain an access token. The access token is stored in the local storage using a simple GET API. My goal is to implement a flag or status at the app. ...

What are the recommended methods for ensuring compatibility of enums in Typescript?

I have a const enum named ComponentId with values A, B, and C. Additionally, there is another const enum called BaseId with values D, E, and F which is used in multiple places. const enum ComponentId { A = 0, B, C } The challenge I am facing ...

Imitating elegant angular input styles

Just before launch, the higher-ups have decided that they prefer a modern aesthetic for input controls - underlines rather than boxes, with labels that slide out of the way when the input is in focus. Check out the effect on this page: https://material.an ...

The Angular 2 dropdown menu isn't populating because of the sluggish http response

I encountered an issue while trying to populate a dropdown list with data from an HTTP response using an Angular2 service. Below is the method in my service: categories: any; getCategories() { let subscription = this.httpclient .get ...

What is preventing the CKEditor 4 Angular module from properly validating form fields?

Why is it that the form field validation for the CKEditor 4 Angular module does not seem to be functioning properly? You can find my code here. I have experimented with different combinations of .touched, .pristine, and .valid. Despite this, the CKEdito ...

Using conditional styles with Angular 2

I am currently working on applying conditional styles to a table. For instance, if a user's "obsolete" property is true, I want to display their information with a <del> tag. I have searched online for solutions, and while I am aware of using *n ...

Using NavParams within a service component,

I'm facing a challenge in accessing NavParams within a provider, and simply importing NavParams is not solving the issue. Here's a brief overview of my application: users input their name and address, a pin is dropped on the map based on the add ...

Is it possible to store all data in the Redux store while using Angular?

As I develop an Angular form, I find myself working with numerous 'select' fields. I'd like to populate these select field options from the server. Would it be advisable to store this data in the ngrx store? Or would using services suffice t ...

Angular routing is giving me trouble when trying to open various menus at the same location

I am new to Angular and facing a situation where my HomeComponent has a reference to the Appmenucomponent in its .html page. I need to be able to click on different menus (each leading to a new component) and have them open in the same place (div), similar ...

`Why isn't GetServerSideProps being triggered for a nested page in Next.js when using Typescript?

I have been working on a page located at /article/[id] where I am trying to fetch an article based on the id using getServerSideProps. However, it seems that getServerSideProps is not being called at all as none of my console logs are appearing. Upon navi ...

Getting and passing data from a frontend form to Java, then displaying it back in the frontend through Angular

The successful implementation of a payment verification SOAP XML API in JAVA has been achieved. Through the use of JAVA code, XML requests are sent to a payment API with SOAPAction headers and Body, resulting in a response from the API. Now, the goal is t ...

Detecting changes in an ondrop event triggered by a modification in object property

As I work on creating a drag and drop interface, I realize the importance of implementing change detection for smooth functionality. Below is a snippet of my component: import { Component, OnInit, ChangeDetectorRef } from '@angular/core'; impor ...

Arranging a dictionary by its keys using Ramda

My task involves manipulating an array of items (specifically, rooms) in a program. I need to filter the array based on a certain property (rooms with more than 10 seats), group them by another property (the area the room is in), store them in a dictionary ...

What is the method for retrieving the name of an object's property within an Angular template

I am trying to display the name of a nested object's property using Angular interpolation <ng-container ngFor="let item of saleDetailsAggegater.productMap | keyvalue"> <tr *ngFor="let qtyMap of item.value | keyvalue"&g ...

Detecting incorrect serialized data entries based on data types

In the scenario where the type MyRequest specifies the requirement of the ID attribute, the function process is still capable of defining a variable of type MyRequest even in the absence of the ID attribute: export type MyRequest = { ID: string, ...