Sequential subscription to an array of observables
Asked Answered
B

2

11

Here, I've used forkJoin from rxjs to subscribe to an array of observables parallelly. But I want to subscribe to them one by one, What will be the best solution?

Below is my code :

var observables = [];

Observable.forkJoin(observables)
    .subscribe(() => {
        this.msgs = [];
        this.msgs.push({
            severity: 'success',
            summary: 'Saved Successfully'
        });
        this.onSaveComplete();
    }, (error: any) => this.errorMessage = <any>error);
}, (error: any) => this.errorMessage = <any>error);
Bluefarb answered 24/9, 2017 at 9:13 Comment(0)
S
9

Alternate of forkJoin means you need to subscribe to the array of observables sequencially. mergeand concat are the ways to go in this case. In your case, modify your code and use a spread operator at the very beginning of your array of observables when using mergeand concat.

var observables = [];
Observable.concat(...observables)
            .subscribe(() => {
                this.msgs = [];
                this.msgs.push({
                    severity: 'success',
                    summary: 'Saved Successfully'
                });
                this.onSaveComplete();
            }, (error: any) => this.errorMessage = <any>error);
    }, (error: any) => this.errorMessage = <any>error);
Siusiubhan answered 25/9, 2017 at 7:29 Comment(0)
E
0

Use the switchMap operator to make a series of dependant requests. Error will propagate and you can catch or otherwise handle them at the end of the chain.

return this.http.get('something/1')
  .switchMap(res1 => {
    // use res1 if you need it
    return this.http.get('something/2')
  })
  .switchMap(res2 => {
    // use res2 if you need it
    return this.http.get('something/3')
  })
  .subscribe(res3 => {
    // use the final response
    console.log(res3)
  })
Editheditha answered 24/9, 2017 at 11:39 Comment(5)
Thanks bro for your reply. I want to do this operation dynamically. I mean the number of observable items may be 2 or 4 or 11 etc, in dynamically how can I solve this problem?Bluefarb
That all depends on your custom logic for using the previous result in determining the next request. I can't help you with more info. If your requests are not dependant of one another, you should be just using forkJoin instead, as there's no reason to wait for one to complete before starting another.Arman
@LazarLjubenović that's just not true. For example if you want to queue a variable amount of http requests to run sequentially as to not ddos an api.Axiology
@AdamKeenan Of course, or maybe a tiger is chasing you and you shouldn't use either operator, but run for your life because it could eat you. Let's not base our arguments on unlikely exceptions just to prove a point. There's a context to the conversation and it doesn't include enormous amounts of requests or extremly dedicated servers.Arman
@LazarLjubenović well I came to this question because I'm in the circumstance I described and I need to know how to do what OP asked. I don't think it's an unlikely exception. Thankfully the other answer mentions concat.Axiology

© 2022 - 2024 — McMap. All rights reserved.