Here's yet another, but I feel more straightforward and intuitive (or at least natural if you're used to Promises), approach. Basically, you create an Observable using Observable.create()
to wrap one
and two
as a single Observable. This is very similar to how Promise.all()
may work.
var first = someObservable.take(1);
var second = Observable.create((observer) => {
return first.subscribe(
function onNext(value) {
/* do something with value like: */
// observer.next(value);
},
function onError(error) {
observer.error(error);
},
function onComplete() {
someOtherObservable.take(1).subscribe(
function onNext(value) {
observer.next(value);
},
function onError(error) {
observer.error(error);
},
function onComplete() {
observer.complete();
}
);
}
);
});
So, what's going on here? First, we create a new Observable. The function passed to Observable.create()
, aptly named onSubscription
, is passed the observer (built from the parameters you pass to subscribe()
), which is similar to resolve
and reject
combined into a single object when creating a new Promise. This is how we make the magic work.
In onSubscription
, we subscribe to the first Observable (in the example above, this was called one
). How we handle next
and error
is up to you, but the default provided in my sample should be appropriate generally speaking. However, when we receive the complete
event, which means one
is now done, we can subscribe to the next Observable; thereby firing the second Observable after the first one is complete.
The example observer provided for the second Observable is fairly simple. Basically, second
now acts like what you would expect two
to act like in the OP. More specifically, second
will emit the first and only the first value emitted by someOtherObservable
(because of take(1)
) and then complete, assuming there is no error.
Example
Here is a full, working example you can copy/paste if you want to see my example working in real life:
var someObservable = Observable.from([1, 2, 3, 4, 5]);
var someOtherObservable = Observable.from([6, 7, 8, 9]);
var first = someObservable.take(1);
var second = Observable.create((observer) => {
return first.subscribe(
function onNext(value) {
/* do something with value like: */
observer.next(value);
},
function onError(error) {
observer.error(error);
},
function onComplete() {
someOtherObservable.take(1).subscribe(
function onNext(value) {
observer.next(value);
},
function onError(error) {
observer.error(error);
},
function onComplete() {
observer.complete();
}
);
}
);
}).subscribe(
function onNext(value) {
console.log(value);
},
function onError(error) {
console.error(error);
},
function onComplete() {
console.log("Done!");
}
);
If you watch the console, the above example will print:
1
6
Done!