Vavr: Does exist examples using Future objects with vavr

Created on 14 Sep 2017  路  2Comments  路  Source: vavr-io/vavr

Hi,

I would like to know if exist any example using Future objects provided by vavr.
I read the documentation, but I have the feeling that the chapter is a bit short.
http://www.vavr.io/vavr-docs/#_future

Juan Antonio

question 芦vavr-concurrent禄

Most helpful comment

Documentation will be part of Vavr 1.0.0. I will close this issue. Thx for asking!

All 2 comments

@jabrena Thanks for asking!

Right, the docs can be improved. We will do that for the 1.0.0 release.

If you know Scala, I would suggest to read the Scala docs on Future/Promise. Vavr works basically the same.

You could take a look at our unit tests FutureTest and PromiseTest.

In fact, Future is a very elegant way to handle async computations in Java. They are composable, e.g.

  • you are able to transform results as if they were available (but aren't yet)
Future.of(() -> /*this takes veeeery looong*/) // but this call does not block
           .map(result -> ...) // the function parameter is called as soon as the result is available
           .onComplete(result -> ...); // don't use get(), it blocks
  • sequence several async computations to a single collection of results that is available when all computations finished
Future<Seq<T>> futures = Future.sequence(future1, future2, ...);

// or

Future<Seq<User>> users = List.of(1, 2, 3).map(userId -> Future.of(() -> db.loadUser(userId)));
  • etc.

Documentation will be part of Vavr 1.0.0. I will close this issue. Thx for asking!

Was this page helpful?
0 / 5 - 0 ratings