【发布时间】:2021-01-26 23:32:56
【问题描述】:
如何使用 Kotlin jdk 异步函数重写此代码以避免错误“只能在协程体中调用暂停函数”?
var result: CompletableFuture<CarInfoDto>? = null
try {
result = CompletableFuture.supplyAsync {
runBlocking {
myService.getCarInfo(carId) //**"Suspension functions can be called only within coroutine body"**
}
}
return ResponseEntity(future?.get(1000, TimeUnit.MILLISECONDS), HttpStatus.OK)
} catch (timeoutException: TimeoutException) {
【问题讨论】:
标签: kotlin asynchronous