Vert.x 是 "based on callbacks"(而不是期货)是什么意思?

What does it mean that Vert.x is "based on callbacks" (instead of futures)?

在谈论期货和回调时,documentation 表示

The Vert.x core APIs are based on callbacks to notify of asynchronous events. The seasoned developer will naturally think that this opens the door to the so-called "callback hell" where multiple levels of nested callbacks render the code difficult to comprehend as illustrated by this fictional code:

foo.a(1, res1 -> {
  if (res1.succeeded()) {
    bar.b("abc", 1, res2 -> {
      if (res.succeeded()) {
         baz.c(res3 -> {
           dosomething(res1, res2, res3, res4 -> {
               // (...)
           });
         });
      }
    });
  }
});

While the core APIs could have been designed to favor promises and futures, the choice of callbacks as the cardinal model is actually interesting since it allows different programming abstractions to be used.

  1. "The Vert.x core APIs are based on callbacks"是什么意思?如果基于期货会有什么不同?
  2. "callbacks as the cardinal model" 是什么意思?
  1. 意味着Vert.x核心异步API都将回调作为输入,并使用回调来处理对[=38=的异步结果的反应] 称呼。如果它基于 Futures,而不是像这样的 API:

      void deployVerticle(Verticle verticle, Handler<AsyncResult<String>> 
          completionHandler);
    

    看起来像这样:

    Future<String> deployVerticle(Verticle verticle);
    

    因此,它 return 不是采用获取 AsyncResult<String> 作为输入的回调,而是 Future<String>。但是,Vert.x team is moving to a hybrid Callback/Future-based model in Vert.x 4.0,所以这两种API在不久的将来都会成为核心Vert.xAPI的一部分。

  2. 我相信这里用cardinal表示"fundamental"。所以它只是意味着 Vert.x 本身在其自己的实现中使用了回调。然后它使用代码生成 and/or 其他技术来派生其他类型的 API 抽象。像 API 那个 return 期货,或 return RxJava types, or can be used as Kotlin coroutines,等等

一般来说,Future 比回调更容易使用,尤其是当您必须将多个异步操作组合在一起时。例如,您在原始问题中包含的代码可能是这样写的,使用 Futures:

CompositeFuture.all(foo.a(1), bar.b("abc", 1), baz.c())
    .compose(fut -> doSomething(fut.resultAt(0), fut.resultAt(1),
        fut.resultAt(2)))
    .compose(res4 -> ...)