error handling - catch-and-replace Rx Observable stream/sequence -
say have observable compromised of 2 remote ajax calls exposed observable's. these streams flatmaps of sponse sequence of values.
var remotedata = rx.observable.merge( data1, data2);
for sake of argument imagine following definitions:
var data1 = rx.observable.from([1,2,3]); // multiple items! var data2 = rx.observable.throw("whoops!");
i either get data1 , data2, fetched or if there any error use cached data.
however if write var dataorcache = remotedata.catch(cacheddata)
append output stream adding cached data after error. because catch
merely continues sequence different observable:
continues observable sequence terminated exception next observable sequence.
this problematic because error might result data2 (or later in data1 steam); yet cachedata should superseded , all results err'ing source.
for example, if cachedata = rx.observable.from(["cached"])
final sequence [1,2,3,"cached"]
when desired goal ["cached"]
.
i've looked @ getting started: errors, unable how handle situation not 1 of listed scenarios. 'caching' situation shown assumes fail-fast observables error before returning any results.
i looking way handle via different stream composition or 1 of built-in operator methods , not want unwrap explicit observable source.
not sure understood :
- if want values cache when ajax call fails, can this:
var remotedata = rx.observable.zip( data1.catch(getfromcache('data1')), data2.catch(getfromcache('data2')) )
this uses instance version of catch
operator, , signature rx.observable.prototype.catch(handler)
: cf. https://github.com/reactive-extensions/rxjs/blob/master/doc/api/core/operators/catchproto.md
your getfromcache
function must return promise or observable. remotedata
emit value when both data1 , data2 fetched, whether cache or ajax call.
- if want take values cache 1 of ajax call fails:
var remotedata = rx.observable.zip(data1, data2).catch(getdatafromcache('data1','data2'))
i haven't tested let me know if worked you.
Comments
Post a Comment