RxJS wait for an observable to complete in its entirety

Viewed 13324

I'm trying to get an Observable to complete in its entirety (meaning the Complete function is called) before the next Observable executes. I've tried many different things, but the closest I've gotten is this:

function() {
  observableA.subscribe(
    (value) => { },
    (err) => { },
    () => {
      createObservableB();
    }
  );
  return observableB; // ????
}

But I need to return the result from createObservableB() from this function. Again, createObservableB cannot be called until every single value in observableA has been iterated over in its entirety.

Thanks for any help!

2 Answers

you can try last operator

obsA.pipe(last(),mergeMap(()=>obsB)).subscribe()

More "clean" solution would be using just concat because it subscribes to Observables one by one only when the previous one completes:

import { concat } from 'rxjs';

concat(observableA, observableB)
  .subscribe(...)

If you want to ignore all values coming from observableA you can use ignoreElements() when passing it to concat (observableA.pipe(ignoreElements())).

Related