Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make an appendable observable queue?

I have an array where I need to make single requests using very single data set of this array. The problem that I found difficult to solve was to schedule the calls. Means, only when a request finishes, the next request starts. I was looking for an RxJs queue, but I couldn't simply find a solution.

Example:

function makeRequest(body): Observable<any> {
   return someAsyncRequest(body);
}

array.forEach((entry) => {
    makeRequest(entry);
});

// This is just an example how the setup is. This code does not work. 
// What I need is a queue like feature in RxJs to append requests and wait before the previous one is finished.

like image 725
Ling Vu Avatar asked Mar 27 '26 15:03

Ling Vu


1 Answers

You have quite few options, but I suppose concat or forkJoin fits you best. concat calls second API only after previous completes, while forkJoin will do the same, but only if none of them errors. If any of them errors, it will not return anything.

Example with concat:

concat(...array.map(entry => makeRequest(entry)).subscribe()

p.s. import concat as static operator:

import { concat } from 'rxjs'
like image 156
Julius Dzidzevičius Avatar answered Mar 29 '26 03:03

Julius Dzidzevičius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!