!(https://hackernoon.com/hn-images/1*HbXol0ztoXNPlJt93R-2sA.jpeg)\n\nImage Credit: [JodiMichelle @ Flickr](https://www.flickr.com/photos/jodimichelle/6421813195/in/photostream/)\n\n> The RxRecipes series generally refers to Android and [RxJava2](https://github.com/ReactiveX/RxJava/tree/2.x), but the same recipes can apply to any Rx implementation and platform ([RxJs](https://github.com/Reactive-Extensions/RxJS), [RxSwift](https://github.com/ReactiveX/RxSwift), a backend with RxJava, etc).\n\n### Rx Composition with External Code\n\nImagine you have a wonderful reactive data layer for your newest application. The API/Disk/Cache setup is beautiful, so fresh so clean, truly an oasis of joy and wonder.\n\nA new requirement comes in from product: you need to initialize the application with some static data to have a wait-free/no-internet-required first run.\n\nSo you decide to throw a JSON document in your /assets folder.\n\nExcept you soon realize this new data source (and associated [AssetManager API](https://developer.android.com/reference/android/content/res/AssetManager.html)) doesn’t fit with your compos-able, schedul-able, chain-able, error-terminating-able, (and all the other-ables) reactive data layer.\n\nWhat we really want is a reactive concise API like this:\n\nBut how do we get there?\n\n!(https://hackernoon.com/hn-images/1*E8ROs4RlEao12IOzP6KX-A.gif)\n\n### Solution: Wrap with fromCallable()\n\nFrom the official documentation:\n\n_Returns an Observable that, when an observer subscribes to it, invokes a function you specify and then emits the value returned from that function. This allows you to defer the execution of the function you specify until an observer subscribes to the ObservableSource. That is to say, it makes the function “lazy.”_\n\n!(https://hackernoon.com/hn-images/1*s3oJcF4MLoyDDqULb4bgJw.png)\n\nUsing fromCallable() is the simplest way to take declarative external code, and make it reactive.\n\nThe fromCallable() creation method expects a callable implementation matching the following simple interface:\n\n### Let’s see the code\n\nI will be using some [Guava](https://github.com/google/guava) classes in this specific implementation (Optional, CharStreams, Lists).\n\n* We return a Single instance which executes the following code when subscribed:\n* We attempt get an InputStream for the file and read the stream into a CharStream which produces a String result.\n* If there is an IOException (file doesn’t exist, some disk issue) we return an absent Optional.\n\n* We create an Observable which executes the following code when subscribed:\n* We get the existing locales for our assets (String\\[\\]).\n* We use the [flatMapIterable](http://reactivex.io/RxJava/javadoc/rx/Observable.html#flatMapIterable%28rx.functions.Func1%29)() operator to transform the stream from String\\[\\] items into a stream of Strings which emits each item in each String\\[\\] (in our case there is just one array in the original stream).\n\n* We return a Completable which executes the close method when subscribed to.\n* Edit: [We are using fromAction() which is like fromCallable() except it accepts a runnable (no return type).](https://github.com/ReactiveX/RxJava/blob/2.x/src/main/java/io/reactivex/Completable.java#L316) fromAction() is still deferred execution, but then we don’t have to do any weird return (thanks [Dávid Karnok](https://medium.com/@akarnokd) for the tip!).\n* Only the Completable type has fromAction() of course, since the other reactive types contain values.\n\n### Why fromCallable() and not just(), or other creation methods\n\nIt’s critical that we use fromCallable() and not something like just(), or fromIterable(), etc. because we don’t want this code to run synchronously.\n\nThis is a critical point, so let’s look at an example:\n\nWe are now blocking the caller thread for a long time. If you are calling this from a the main thread in Android, you’ve just caused an [ANR](https://developer.android.com/training/articles/perf-anr.html), or caused the UI to freeze and become unresponsive.\n\nBut even if you called this from a thread that you don’t mind blocking there are still issues.\n\nImagine this file get’s modified earlier in chain of operators, that change won’t be reflected here. The file could even be deleted after the source is created, and the reactive source won’t reflect that.\n\n!(https://hackernoon.com/hn-images/1*b0mlkp47zCcCpIzDVhM-Dw.gif)\n\nWords are hard…\n\nTo put it another way: The reactive source that we create will emit the result of the operations that already ran when the source was created; NOT run the operation when the source is subscribed to and then emit the result.\n\nThat is a bit of a mouthful, but understanding the difference is crucial in creating Reactive applications, or wrapping imperative code.\n\n### Let’s Review\n\nWe often need to interface some reactive code with imperative code we don’t own (libraries, operating system, internal shared libraries across projects).\n\nWe can wrap the imperative code with fromCallable() to create observable stream sources.\n\nWe reviewed an example Rx-fromCallable()-wrapped implementation of some of the AssetManager API.\n\nfromCallable() (and [fromAction() for Completables](https://github.com/ReactiveX/RxJava/blob/2.x/src/main/java/io/reactivex/Completable.java#L316)) creates a reactive source that executes it’s work when subscribed to, not when we create it.\n\n#### Next in the RxRecipes Series:\n\n!(https://hackernoon.com/hn-images/1*skNf8P_lWPjH4FW1sY9Lsg.gif)\n\nTBD — I’ll update this with a link to the next post once it’s done.\n\nEnjoy the recipe?\n\nDisagree with the implementation?\n\nHave burning-hot fiery opinions about some random sentence above?\n\nI’d love to hear from you, leave your comments below!