Making multiple HTTP requests in succession in Angular with varying quantities

Hello everyone, I have a question to ask and I hope it's clear enough for you all.

My goal is to send a series of requests one after the other (could be a large number) in order to retrieve data.

The data obtained from each request should be gathered and returned as a single observable once all requests are completed.

I tried using forkJoin to merge the requests, but it doesn't guarantee sequential execution, and also considered concat, which emits an observable after each request.

Answer №1

If you need to:

  • Execute multiple HTTP requests in sequence (perhaps based on an array)
  • Collect and return the results in an array

One approach is to use concat alongside toArray. With this setup, concat will ensure sequential execution of requests, while toArray will aggregate responses into an array.

// dynamic array containing values
const source = [ 1, 2, 3, 4, 5 ];

const observables = source.map(x => this.http.get('some url'));
concat(
  ...observables
).pipe(
  toArray()
).subscribe(responses => {
  console.log(responses);
  // array of collected results
});

Try it out: https://stackblitz.com/edit/angular-s1fdxj

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

When clicking on OpenLayers Map in Angular, the value may not update as expected or may be set back

My initial project incorporates OpenMaps / Openlayers. The specific component I am referring to appears as follows: import {AfterViewInit, ChangeDetectionStrategy, Component} from '@angular/core'; import {Map, MapBrowserEvent, View} ...

Angular2 - Model not being refreshed by Directive

I have implemented a directive on an HTML input box to handle numeric values. The directive is meant to "clean" the number (removing commas, dollar signs, etc) when a user pastes a number into the textbox. Although the cleaning code works properly, the iss ...

An issue arose when attempting to compare '[object Object]'. Please note that only arrays and iterables are permitted (Angular 5)

I am facing an issue while attempting to display array data in a standard HTML table. There is a method called soa.service that retrieves data in an array format. service. get(Type: number, Code: string): Observable<Items[]> { var requestOptio ...

Using CanActivateGuard with Data Transfer to the Guard

In my angular app, I have integrated an authentication guard to handle user access. The app consists of different modules, each utilizing a factory to create a unique ThemeService. These modules are self-contained applications with distinct styles and secu ...

Retrieve a specific JSON object by ID in an Angular Typescript observable

Below is a json string that contains both "stocks" and "contacts" data arrays. Depending on the request, I need to extract either the "stocks" or "contacts" data: [{ "id": "stocks", "name": "Stocks", "data": [ { "id": 1, "name": "Act ...

Retrieve the stored information from a variable

Can anyone help me with a coding issue I'm facing? I have a constant called data, which contains two values - prediction and probability. What is the best way to access and retrieve both values? type ML = { prediction: string; probability: num ...

Using React and TypeScript, open the initial tab from a mapped array with an accordion component

{accordion.map(accordionItem => ( <AccordionItem key={accordionItem.title} text={accordionItem.text} title={accordionItem.title} ></AccordionItem> ...

Seeking the Origin of NgxMqttServiceConfig Import: Dealing with NullInjectorError in Angular Testing

While working on an application using Angular 8 and ngx-mqtt, I encountered an error when running the tests defined in the .spec.ts files. The error message reads: NullInjectorError: StaticInjectorError(DynamicTestModule)[InjectionToken NgxMqttServ ...

A guide on how to implement promise return in redux actions for react native applications

I'm using redux to handle location data and I need to retrieve it when necessary. Once the location is saved to the state in redux, I want to return a promise because I require that data for my screen. Here are my actions, reducers, store setup, and ...

Steps to develop a sub-route specifically for a single word

Take a look at this code: {path : 'recipes', component:RecipesComponent, children:[ {path:':id', component:RecipeDetailComponent}, {path:':new', component:NewRecipeComponent } ]}, No matter which link you use: h ...

Issues have arisen where the modal popup in Bootstrap and Angular is failing to appear

Displayed below is a table with a modal popup created using bootstrap 4 within an angular environment. <tbody *ngFor="let data of json | sizeFilter : sizeInput"> <tr> <td (click)="onUpdateClick(data.hash , data. ...

Encountered an issue while configuring the Apollo server - The type '() => void' cannot be assigned to type '() => DataSources<object>'

I need help with a TypeScript-related issue. I am struggling to implement the expected return type for the function dataSources in this scenario. Here is the code snippet: const dataSources = () => { quizzessApi: new QuizzessDataSource(); } const ...

Require fields in TypeScript interfaces only for array types

Is there a way to make only array type interface fields required, not all of them? The Required operator currently makes every field mandatory, but I specifically need just the array fields to be required. ` interface IExample { a: number, b?: str ...

React with Typescript - cannot be expressed as a function

I am currently exploring ReactJS and Typescript. While trying to authenticate a user using the methods below, I encountered the following error message: Unhandled Rejection (TypeError): auth.authenticate is not a function onSubmit src/components/Login/ind ...

Is it possible to integrate Symfony, Node.js, and Angular into a single application?

Hey there, I am currently working on an audit platform for applications developed using Angular, Node.js, and Symfony. I was wondering if it is possible to use all three technologies simultaneously. You can check out my project in the link below. Thank you ...

An undefined variable in Angular 2's evaluation

I am currently working with the following code: @ViewChild('MondayPicker') MondayPicker; @ViewChild('TuesdayPicker') TuesdayPicker; @ViewChild('WednesdayPicker') WednesdayPicker; @ViewChild('ThursdayPicker') Thursda ...

Separating Angular code into distinct components

My page contains two input fields: one is for email and the other is a text field. Currently, everything is functioning correctly. However, I now want to split the component into two parts. For example, I have a 'basic-info' component currently. ...

Error in Typescript: A computed property name must be one of the types 'string', 'number', 'symbol', or 'any'

Here is the current code I am working with: interface sizes { [key: string]: Partial<CSSStyleDeclaration>[]; } export const useStyleBlocks = ( resolution = 'large', blocks = [{}] ): Partial<CSSStyleDeclaration>[] => { cons ...

Error Message: Unable to Modify Headers Once Sent - NodeJS

I encountered an issue with my node.js app where one of the routes keeps throwing a "Can't set headers after they are sent error". Description of the route: In my application, users have different access levels. This specific route checks the user&a ...

Transform the data type from "any" to an Object

Currently, I am enrolled in an online Angular course. However, the course is teaching an older version of Angular and I have been able to adapt up until Angular 7. Now I am facing a particular issue: In the component script, there is a Vehicle object defi ...