Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS wait for an observable to complete in its entirety

Tags:

rxjs

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!

like image 371
Nick Avatar asked Oct 30 '18 23:10

Nick


People also ask

How do you wait for an Observable to finish?

You can also try operators like switch or switchmap according to your requirements. Show activity on this post. async main(data: string): string { var process1Data: string = await process1(data). toPromise(); var process2Data: string = process2(process1Data); ... }

Does forkJoin wait for all observables?

forkJoin will wait for all passed observables to emit and complete and then it will emit an array or an object with last values from corresponding observables.

Can I use await on Observable?

You can use Observables with Promises and with async/await to benefit from the strengths of each of those tools.

How do you finish Observable?

takeUntil(end$); When you want to end observable , you do end$. onNext("anything you want here"); . That is in the case the ending event is generated by you.


1 Answers

you can try last operator

obsA.pipe(last(),mergeMap(()=>obsB)).subscribe()
like image 183
Fan Cheung Avatar answered Nov 17 '22 04:11

Fan Cheung