I am somewhat new to RxJs and I am trying to mix the world of promises and observables.
Here is what I want:
I have an observable (call it clickObs
) which listens to a click and as a result interrogates a database, producing a promise which resolves to a value when the database querying concludes (successfully).
My observable thus generates a stream of promises from a stream of clicks, and what I want is to generate from that observable, a stream of corresponding resolved values.
From past questions on stackoverflow, I read about defer
, flatMap
, mergeAll
, and fromPromise
, but cannot get my head around how to articulate the four to solve my problem.
Any suggestions?
You shouldn't need all four, just look at flatMap
or its sibling 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 a promise or array-like object into an Observable
and flatten out the resulting sequence. FlatMapLatest
is similar but will ignore old events, if a newer one arrives before the previous one completed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With