Convert Promises stream to value stream

I'm a little new to RxJs, and I'm trying to mix the world of promises and observables.

Here's what I want: I have an observable (call it clickObs ) that listens for a click and as a result interrogates the database, creating a promise that resolves the value when the database query completes (successfully). Thus, my observable generates a stream of promises from the click stream, and I want to create a stream of corresponding allowed values ​​from this observable.

From past stackoverflow questions I read about defer , flatMap , mergeAll and fromPromise , but I can't figure out how to formulate four to solve my problem.

Any suggestions?

+4
source share
1 answer

You don’t need all four, just look at flatMap or his brother flatMapLatest

 clickObs.flatMapLatest(function() { //Access the db and return a promis return database.query(queryObj); }) .subscribe(function(result) { //Result is implicitly flattened out /*Do something with the result*/ }); 

flatMap will implicitly convert the promise object or array into an Observable object and smooth out the resulting sequence. flatMapLatest similar, but will ignore old events if a new one is executed before the previous one completes.

+2
source

All Articles