【发布时间】:2020-09-30 18:49:20
【问题描述】:
我有一个如下所示的同步函数:
void doStuff(int x, String y, Consumer<String> onSuccess, Consumer<Throwable> onFail) {
// start something that happens on other Threads using RxJava that eventually either
// 1) Calls onSuccess(String) or onFail(Throwable)
// 2) returns to caller right away after starting this mess off
}
我想为此编写一个同步调用它的包装器。我已经编写了一个名为Result 的类来返回给调用者。它将有一个String 或一个Throwable。我要做的是弄清楚如何调用异步函数并使用它的onSuccess 和onFail 参数来表示doStuff 函数应该退出返回正确的Result。
Result doStuffSync(int x, String y) {
// magic that calls `doStuff` with callbacks that enable the entire thing to run
// within this function and build a Result
return result;
}
【问题讨论】:
标签: java asynchronous