Adapting JavaScript APIs for RxJS

Just like in the world of .NET, APIs that are available to us in the browser aren't RxJS-aware by default. However, JavaScript actually has an advantage compared to .NET—every API that does something non-trivial like write to a file or send a network request is asynchronous, via a callback method.

We've already seen how to wrap asynchronous methods like this via the FromCallbackPattern() example, and in RxJS it's even easier. In the following, let's take a look at how we could wrap a new API, the HTML5 Geolocation API:

getCurrentPositionRx = function(opts) {   opts = opts || {};   var ret = new Rx.AsyncSubject();   // Our callbacks will just OnNext the Subject, similar to how FromAsyncPattern   // works.   navigator.geolocation.getCurrentPosition( ...

Get Programming Reactive Extensions and LINQ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.