Muhammad Hammad
Muhammad Hammad

Reputation: 87

How can I let an observable to wait before firing the next event

I am using RxJs in an Angular project. There are two subjects

    s1.next()

    s1.subscribe(() => {
      // does some operation and then triggers s2 event
      s2.next()
    });

    s2.subscribe(() => {
      // does some operation and then ends.
    });

How can I let s1 to wait until s2 finishes its job.

Upvotes: 0

Views: 345

Answers (1)

satanTime
satanTime

Reputation: 13539

In your code you need to show what kind of job is that. I think you don't need s2, you need to build a pipe based on s1 emits.

if it's a request - simply make a pipe

s1.pipe(
  // s1 emits
  switchMap(() => {
    return this.http.get('url1');
  }),

  // one more request, once the first one has been finished.
  switchMap(() => {
    return this.http.get('url2');
  }),

).subscribe(response => {
  // 2 requests have been completed and we are here.
});

s1.next(); // triggers the flow.

Upvotes: 2

Related Questions