RxJs - How to implement parallel HTTP requests when you have a variable number of requests to make

codependent

I am developing a Node.js API with Express, using the node-rest-client module to make http requests.

One of the API endpoints to develop is /api/v1/users/:userId that returns the full information of a user, its user info plus the detailed information about the departments he belongs to.

To get the information there are this backend REST services:

/users/:userId - Returns a JSON object with the user info plus the list of department ids, e.g.:

{ "name" : "xxx",
  "departments" : [1, 5 ,6, 8]
}

/departments/:departmentId - JSON object with the department info

{ 
  "id" : x,
  "name" : "xxx"
}

An invocation to /api/v1/users/1would need to call

  1. GET /user/1 -> { "name" : "user1" , "departments" : [1, ,5 ,7 ,8]}
  2. Get the department ids and make n calls to /departments/deparmentId
  3. After all the calls, compose the full JSON response and return it.

I would like to paralellize the requests using RxJs, so I guess it would be enough with using Rx.Observable.zip().

The point is, if I have an array of Observables, whose size is not fixed, representing every HTTP request call, how can I invoke Observable.zip()?

If the number of elements in the array where fixed I woud do it like this:

var observables = [ obs1, obs2 ];

Rx.Observable.zip( observables[0], observable[1], function(...){...});

But I don't know how many observables there are, so how can I call zip()?

Alexandru Olaru

You can use the ... operator from es6,

var observables = [obs1, obs2, obs3, ...,  obsx];
Rx.Observable.zip(...observables, function() {});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Make multiple http requests in parallel

How do you make HTTP requests with Raku?

RxJS - Combine Two HTTP GET Parallel Requests

How to Make Numerous HTTP Requests Using Task Parallel Library

How to make a sequence of http requests in Angular 6 using RxJS

Limit number of parallel http requests in node js

How to know when parallel HTTP requests are fulfilled in iOS?

How to make multiple http requests?

How to send 1000 XHTTP Requests with maximum number of parallel requests

Parallel HTTP requests with Retrofit

Angular - How to implement switchMap for multiple http requests?

How to implement a "delivery receipt" for http requests

How many http requests do I make, when I request the document page with Requests in Python 3.9?

How to make http requests using HTTP Intercepters

How to make multiple HTTP requests with RxJS and merge the response into one payload and map it to another action?

RxJS - Using zip to parallelize deferred Observables that make HTTP requests not working - You provided an invalid object where a stream was expected

Maximize number of parallel requests (aiohttp)

Make curl parallel requests faster

How to call several http requests in parallel(with retry functionality) and await when all of them are finish?

Parallel HTTP requests in Angular 4

Limit number of requests at a time with RxJS

RxJS how do I make POST requests in sequence where a new request is fired only when the previous completed?

RxJS batch HTTP requests in Angular

Performing advanced http requests in rxjs

Multiple HTTP Requests with RxJS, with parameters

RXJS - multiple consecutive http requests

How to make concurrent HTTP requests with basic authentication

How to make HTTP requests using Plink

How to make HTTP client requests with Deno