Tag: kovenant

链接承诺自定义错误类型与Kovenant

我正在使用Retrofit来访问一个REST API,我想用Kovenant的函数来链接几个工作单元。 换句话说,我想将一个API调用的输出作为参数提供给另一个API调用。 我已经定义了一个自定义的错误类型,它包含一个属性来包含API特定的错误代码,定义如下: class ApiError(val statusCode: Int, val apiErrorCode: Int, val message: String) 如果在API调用期间发生错误,则使用适当的错误代码创建此类的实例。 根据Kovenant的文档,我可以创建一个Deferred<V, E>的实例来获得Promise<V, E> 。 我希望我可以提供泛型参数E ApiError 。 我没有看到Kovenant的Deferred定义中的任何通用约束,似乎要求E是一个Exception 。 这就是说,当我试图链接两个函数返回Promise<V, ApiError>使用, then我收到一个编译器错误,说明预期的类型, then是Promise<V, Exception> 。 我可以使Kovenant工作与自定义错误类型,如果不是,是否继承异常包括属性我需要正确的解决方案,我试图实现?

Chaining承诺在Kovenant中返回职能

我试图连锁2承诺与Kovenant功能,如下所示: fun promiseFunc1() : Promise<Int, Exception> { return Promise.of(1) } fun promiseFunc2() : Promise<Int, Exception> { return Promise.of(2) } fun promiseCaller() { promiseFunc1() then { it: Int -> promiseFunc2() } then { it: Int -> // Compilation error: it is not not an integer } } Kovenant似乎回报了价值。 我如何从promiseFunc2得到实际的整数? 我得到的唯一解决方案是使用get()函数,如下所示: promiseFunc1() then { it: Int -> […]

在Kotlin使用Kovenant Promise.of(价值)有时我泄漏异常

使用Kovenant,我Promise.of(value)使用Promise.of(value)函数来创建一个同步结果,我想把它作为一个承诺来包装。 但是有时候会这样叫: Promise.of(callSomeCalculation()) <– throws exception sometimes .then { … } .then { … } .fail { ex -> log.error(ex) } <– exception is not logged 这段代码丢失了在第一次承诺期间发生的异常。 他们去了哪里? 他们从不记录。 有时他们只是崩溃我的应用程序与未处理的异常。 为什么这个承诺不能抓住他们呢? 注意: 这个问题是由作者故意编写和回答的( 自我回答问题 ),所以在SO中共享有趣问题的解决方案。

在Kotlin中,我如何整合Kovenant承诺和Elasticsearch异步响应?

我在我的Kotlin应用程序中使用Kovenant,我打电话给Elasticsearch,它有自己的异步API。 我宁愿使用承诺,但最好的我可以想出如下: task { esClient.prepareSearch("index123") .setQuery(QueryBuilders.matchAllQuery()) .execute().actionGet() } then { … } success { … } fail { … } 这使得Kovenant异步任务线程,然后Elasticsearch使用其池中的线程,然后actionGet()同步阻止Elasticsearch取回结果。 在阻塞其他人的时候产生新的线程似乎很愚蠢。 有没有一种方法可以更紧密地集成线程调度? 注意: 这个问题是由作者故意编写和回答的( 自我回答问题 ),所以在SO中共享有趣问题的解决方案。

从Kotlin使用Kovenant我不断使用延迟重复代码

当使用Kotlin的Kovenant时,我得到了很多代码: fun foo(): Promise<SomeResultType, Exception> { val deferred = deferred<SomeResultType, Exception>() try { // bunch of work that deferred.resolve() or deferred.reject() } catch (ex: Exception) { deferred.reject(ex) } return deferred.promise } 我知道我可以使用task { … }作为异步承诺,并会照顾这一些,但是当我已经是异步或写适配器到其他异步类我想使用Deferred实例。 有没有像task { … } ,使延期,而是照顾所有额外的工作? 注意: 这个问题是由作者故意编写和回答的( 自我回答问题 ),所以在SO中共享有趣问题的解决方案。