Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make one Observable sequence wait for another to complete before emitting?

Say I have an Observable, like so:

var one = someObservable.take(1);  one.subscribe(function(){ /* do something */ }); 

Then, I have a second Observable:

var two = someOtherObservable.take(1); 

Now, I want to subscribe() to two, but I want to make sure that one has completed before the two subscriber is fired.

What kind of buffering method can I use on two to make the second one wait for the first one to be completed?

I suppose I am looking to pause two until one is complete.

like image 346
Stephen Avatar asked May 29 '15 01:05

Stephen


People also ask

Which operator allows you to wait for a defined delay until an item is emitted?

The Delay operator modifies its source Observable by pausing for a particular increment of time (that you specify) before emitting each of the source Observable's items.

How do you call one Observable after another?

Make a single observable out of the several ones that need to be executed in parallel (i.e. the many deletions), using forkJoin. Use switchMap to execute one observable after another.

What is defer in RXJS?

defer allows you to create an Observable only when the Observer subscribes. It waits until an Observer subscribes to it, calls the given factory function to get an Observable -- where a factory function typically generates a new Observable -- and subscribes the Observer to this Observable.


1 Answers

A couple ways I can think of

import {take, publish} from 'rxjs/operators' import {concat} from 'rxjs'  //Method one  var one = someObservable.pipe(take(1)); var two = someOtherObservable.pipe(take(1)); concat(one, two).subscribe(function() {/*do something */});  //Method two, if they need to be separate for some reason var one = someObservable.pipe(take(1)); var two = someOtherObservable.pipe(take(1), publish()); two.subscribe(function(){/*do something */}); one.subscribe(function(){/*do something */}, null, two.connect.bind(two)); 
like image 72
paulpdaniels Avatar answered Nov 09 '22 22:11

paulpdaniels