https://kotlinlang.org logo
#arrow
Title
r

raulraja

04/27/2018, 11:29 PM
The arrow way combined with
DeferredResult
from Spring would be to have your program in terms of
IO
or
Effect
so you can run it non blocking. Something along the lines of:
Copy code
import <http://arrow.effects.IO|arrow.effects.IO>

/** transforms IO into a deferred result **/
fun <A> IO<A>.toDeferredResult(): DeferredResult<A> { 
  val deferredResult = new DeferredResult<ResponseEntity<A>>()
  unsafePerformAsync {
     it.fold(deferredResult.setResult, deferredResult.setResult)
  }
  return deferredResult
}

fun getCustomer(customerId: String): DeferredResult<ResponseEntity<CustomerResponse>> =
  IO { getCustomerService().getCustomer(customerId) }
    .attempt()
    .map {
      it.fold({ 
        ResponseEntity(ErrorResponse(), HttpStatus.INTERNAL_SERVER_ERROR)
      }, { 
        ResponseEntity(CustomerResponse(it) , HttpStatus.OK) 
       })
    }.toDeferredResult()