Book Image

Learning Reactive Programming with Java 8

By : Nickolay Tzvetinov
Book Image

Learning Reactive Programming with Java 8

By: Nickolay Tzvetinov

Overview of this book

Table of Contents (15 chapters)
Learning Reactive Programming with Java 8
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Other Observable factory methods


Here, we will inspect a few methods that can be used in combination with transforming operators such as flatMap or combining operators such as .zip file (more about this in the next chapter).

In order to examine their results, we will use the following method for creating subscriptions:

void subscribePrint(Observable<T> observable, String name) {
  observable.subscribe(
    (v) -> System.out.println(name + " : " + v),
    (e) -> {
      System.err.println("Error from " + name + ":");
      System.err.println(e.getMessage());
    },
    () -> System.out.println(name + " ended!")
  );
}

The idea of the preceding method is to subscribe to an Observable instance and label it with a name. On OnNext, it prints the value prefixed with the name; on OnError, it prints the error together with the name; and on OnCompleted, it prints 'ended!' prefixed with the name. This helps us debug the results.

Note

The source code of the preceding method can be found at...