How to return Deferred with the instant result?

Viewed 1928

I have a conditional statement which should return Deferred result in both cases, but I don't know how to manually create Deferred object with predefined result. Here's code snapshot:

val deferredResult = if (condition) {
   callSuspendFunction(params)
} else {
  deferred???
}

callSuspendFunction returns the result of async operation. Although I can write something like coroutineScope { async { result } } instead of deferred???, but it looks really not what it should be.

2 Answers

you can drictlly use like below. In first condition it will return int and in second case it will return boolean

   val deferredResult = if (condition) {
        3
    } else {
      true
    }
Related