cadence-workflow - 如何在不等待的情况下获得 Activity 的结果?以事件驱动的方式
问题描述
团队,我对并行调用活动有疑问。
我知道可以使用Async.function
.
对于我的用例,我想在收到所有活动的结果后并行调用 n 个活动,我想进入下一个状态,它可以是另一个活动或决策任务。
但是这里它给了我一个类似于 Java 中的 Future 的 Promise 对象。如果我们编写 promise.get() 来获得结果,但它阻塞了线程。
我需要类似于事件驱动的时尚而不阻塞线程。
希望你能理解我的问题!!
解决方案
您不必使用.get()
.
在Promise中,您可以注册一个回调函数,以便在结果可用或返回错误时调用。
您可以根据需要使用以下方法之一:
- 然后申请
- 处理
- 然后撰写
- 异常地
这种风格类似于 Java 的CompletableFuture。
/**
* Returns Promise that contains a result of a function. The function is called with the value of
* this Promise when it is ready. #completeExceptionally is propagated directly to the returned
* Promise skipping the function.
*
* <p>Note that no blocking calls are allowed inside of the function.
*/
<U> Promise<U> thenApply(Functions.Func1<? super V, ? extends U> fn);
/**
* Returns Promise that contains a result of a function. The function is called with the value of
* this Promise or with an exception when it is completed. If the function throws a {@link
* RuntimeException} it fails the resulting promise.
*
* <p>Note that no blocking calls are allowed inside of the function.
*/
<U> Promise<U> handle(Functions.Func2<? super V, RuntimeException, ? extends U> fn);
/**
* Returns a new Promise that, when this promise completes normally, is executed with this promise
* as the argument to the supplied function.
*
* @param fn the function returning a new Promise
* @param <U> the type of the returned CompletionStage's result
* @return the Promise that completes when fn returned Promise completes.
*/
<U> Promise<U> thenCompose(Functions.Func1<? super V, ? extends Promise<U>> fn);
/**
* Returns a new Promise that, when this promise completes exceptionally, is executed with this
* promise's exception as the argument to the supplied function. Otherwise, if this promise
* completes normally, then the returned promise also completes normally with the same value.
*
* @param fn the function to use to compute the value of the returned CompletionPromise if this
* CompletionPromise completed exceptionally
* @return the new Promise
*/
Promise<V> exceptionally(Functions.Func1<Throwable, ? extends V> fn);
推荐阅读
- typescript - EmptyError:在角度保护中使用 takeWhile 时没有序列中的元素
- html - 在Angular 6中折叠/展开嵌套的div
- mdx - MDX 将集合除以度量
- solr - 使用 Solr 自定义预输入
- bash - 如何在 sed 中处理 UTF-8 表情符号?
- javascript - 如何在按钮按下时触发警报功能?
- php - PHP/SQL - 查询关系数据库的 ID,但能够使用 ORDER BY 在单独的数据库中查询这些 ID
- java - Docker 上的 SpringBoot AMPQ 无法始终连接到队列
- delphi - 如何在 RAD Studio XE5 中启动或启用欢迎页面
- excel - VBA 循环中缺少数字会产生错误