ReactiveX / RxJava

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
Apache License 2.0
47.88k stars 7.61k forks source link

Promises based on RxJava #5784

Closed SMontiel closed 6 years ago

SMontiel commented 6 years ago

What do you all think about Promises based on RxJava, buddies? Some like this:

Promise<String> promise = Promise.resolve("Hello world!");
promise.then(new Consumer<String>() {
        @Override
        public void accept(String s) {
            System.out.println(s);
        }
    }).then(new Function<String, Boolean>() {
        @Override
        public Boolean apply(String s) {
            return s.equals("Hola mundo");
        }
    }).then(new Consumer<Boolean>() {
        @Override
        public void accept(Boolean bool) {
            System.out.println(bool);
        }
    }).done();

Output:

Hello world!
false

Is it a good idea add it into RxJava or as a separated library?

akarnokd commented 6 years ago

It is beyond the scope of this project and I think RxJava offers more behavior, such as retrying a flow which you don't have to rebuild from scratch.

SMontiel commented 6 years ago

Well it is true, but Promises based on RxJava could offer more behavior. So I will develop as a separated library, just to have something similar to JavaScript promises. :smile: Or what's your opinion?

akarnokd commented 6 years ago

They exist as CompletableFutures and CompletionStage in Java at least and there exist several backports. I think learning the ReactiveX style of asynchronous programming first helps you understand Promises and other one element async approaches better. Of course, for educational purposes, you are welcome to implement your own library, which can give you nice insights into the standard concurrency abstractions and tools of Java.

SMontiel commented 6 years ago

But that was added to Java 8, right? There is nothing for Java 7 or 6, or am I wrong?

akarnokd commented 6 years ago

CompletableFuture backport.

SMontiel commented 6 years ago

I am starting to develop the library, I'll do it based on RxJava's architecture, just to have something closer to JavaScript's promises and to learn the core concepts of RxJava. I will show you when it's done. Thank you for your time more than anything else! :smile:

akarnokd commented 6 years ago

Good luck!

SMontiel commented 6 years ago

It is done, @akarnokd! Check it out here! Documentation are missing. Some feedback?

akarnokd commented 6 years ago

It pretty much looks like Single without cancellation support. Also afaik promises are supposed to be hot, i.e. representing an ongoing or terminated computation.

SMontiel commented 6 years ago

Mmm yeah, it is like Single I was thinking about that if it have to be cold or hot, I'll be working on that. When you say 'representing or terminated computation' you refer to Promise's states(FULFILLED, REJECTED and PENDING? In the first call to done() I'll be caching that value(value or Throwable) and state, so next calls will be returning that value or Throwable, this is my idea. am I right? do you have a better idea (yes, you have)?

akarnokd commented 6 years ago

I meant what's written here.

For such cached state, there is the SingleSubject.