what is the usage of completer method of future in vertx?

mj. rahmati :

I'm new in VertX framework (and reactive programming too). when I read about 'Future', I don't understand what is the usage of the 'completer' method, and when do must be used from that? I will appreciate if someone could help me?

tsegismont :

Vert.x asynchronous methods are often defined using a Handler<AsyncResult<X>> parameter. This parameter is the callback invoked when the operation completes.

vertx.createHttpServer().listen(ar -> {
  // callback implementation
});

As you noticed, in the latest versions of Vert.x, a Future<X> implements Handler<AsyncResult<X>> so you can create a future and use it directly as an asynchronous operation parameter:

Future<HttpServer> serverFuture = Future.future();
vertx.createHttpServer().listen(future);

And then you can use the future methods like map or compose.

In older versions, a future wasn't a handler for asynchronous results, so you had to use the completer method:

Future<HttpServer> serverFuture = Future.future();
vertx.createHttpServer().listen(future.completer());

As of 3.7.0, the completer method is deprecated and will be removed in version 4.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=329631&siteId=1