O'Reilly logo

Programming Reactive Extensions and LINQ by Paul Betts, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required