Introduction to the Reactive Extensions for JavaScript – Error Handling Part II

We’ve covered a bit recently with conditional and looping operators on the Reactive Extensions for JavaScript, but I want to step back just a minute and cover exception handling. This post will cover how we can compensate for errors as they happen in several ways and will largely follow Bart de Smet’s post on the same topic, but instead of covering the Interactive Extensions, we’ll stick primarily in JavaScript. Last time, we covered the basics of error handling including Catch and Finally.

OnErrorResumeNext VB Style

The next error handling feature in the Reactive Extensions for JavaScript is one that should be familiar with those in the Visual Basic world, called OnErrorResumeNext (On Error Resume Next). In Visual Basic parlance, this meant you specify that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred, and execution continues from that point. The functionality is similar to the Concat operator which allows you append Observable sequences onto each other to create a single sequence, except that should an error occur, it is not bubbled up and instead continues to the next. This operator comes in two flavors, one as a static method and one as an instance method:

Let’s create a simple example where we want to output the numbers 1 through 9, even if an exception occurs along the way. In this case, we yield two arrays and the third then has an exception concatenated to the end before then yielding the final array.

This is great for handling data that may be potentially bad and is ok to drop the exception should it occur.

Cleaning Up With Using

Just as in .NET there is the using keyword to denote a try/finally with a call to Dispose, we need a similar capability in the JavaScript world for resource cleanup in the asynchronous world. Let’s look at the signature of the function which takes a resource selector which returns the Disposable object, and then the usage which takes the Disposable object and you return an Observable sequence.

A Disposable object is nothing more than an object with a Dispose method which takes no arguments and returns nothing. For example, we could create a Disposable object to encompass the jQuery data API which allows us to store arbitrary data associated with a given element. This object would allow us to both get and set the value, and in our Dispose, we would remove the data.

Using this, we can write two samples, the first of which sets the data and then retrieves the data without an exceptional condition whereas the second example does. let’s take a look at the first example which yields to us the answer of 56088.

In both cases here, we ensure that the underlying data is properly disposed when we leave the scope of our Using operator.

If At First You Fail, Retry

Let’s move onto the last operator for this section on error handling, Retry. The idea behind this operator is that we can retry the observable sequence until it succeeds (meaning no exception) or hits the maximum try count. Let’s look at the method signatures below:

However, if we indeed had a collection that did throw an exception each and every time, we could run into a big issue such as the following, where it would blow out the JavaScript stack as it would try to go on forever:

Luckily, we have ways around this where we can specify the maximum number of retries that are allowed with the provided overload. Next, let’s show a case where Retry is really used. For example, let’s say we have a loop which could produce three values, but will fail at different points for the first two iterations.

In this situation, we will render the following result, since we hit two different exceptions:

4// Hits exception45// Hits exception456

And there you have it, we have composable ways around potential failures in our asynchronous push based operations that give us the flexibility that our synchronous imperative ones do.

Conclusion

Dealing with asynchronous programming has been in the forefront of many minds in the JavaScript community. At JSConf, there were several examples of frameworks trying to get around the idea of callbacks and instead lean more towards composition. By utilizing the Reactive Extensions for JavaScript, we’re able to compose together asynchronous and event-based operations together and transform the results in interesting ways.

When we start creating more advanced workflows through the Reactive Extensions, we also need ways of handling errors as well. We have the ability do handle errors and compensate as they happen through a rich set of operators including Catch, Finally, Using, OnErrorResumeNext, Retry and more.