Disclaimer: it is the continuation for the previous Safe update for 2 dependent streams question
What is the idiomatic way to handle errors in RxJS (or any other RX implementation) that allows the stream to not terminate?
Relevant code is
function convert(unit, value) {
var request = {};
request[unit] = value;
var conversion = $.ajax({
method: 'POST',
url: './convert.php',
data: request,
dataType: 'json'
}).promise();
return Rx.Observable.fromPromise(conversion).takeUntil(inInput.merge(cmInput));
}
var cmElement = document.getElementById('cm'),
inElement = document.getElementById('in');
var cmInput = Rx.Observable.fromEvent(cmElement, 'input').map(targetValue),
inInput = Rx.Observable.fromEvent(inElement, 'input').map(targetValue);
var inches = cmInput
.flatMap(convert.bind(null, 'cm'))
.startWith(0);
var centimeters = inInput
.flatMap(convert.bind(null, 'in'))
.startWith(0);
So as you can see we use the stream of input field changes and pass it through the convert
function that converts it into another unit and passes the result further.
If the error during $.ajax()
call occurs then it's propagated up and the whole inches
or cetimeters
stream stops (it actually is expected).
But how would I implement it to not do so?
So that I could handle error gracefully, like show error message and try again when new data arrives?
My current idea is to introduce a composite type like Haskell's Data.Either
and stream it instead of scalar doubles.
Thoughts?
UPD: Yes, I've read Handling Exceptions in Reactive Extensions without stopping sequence but I still hope there are better ways.
targetValue
? – Carleton