# ktor

Alex Cruise

12/21/2022, 10:25 PM
hey folks, I couldn’t find info about this in the docs, but I’m implementing an API that’s semi-async, i.e. the client can specify that if the request completes within a particular timeout, the server can respond synchronously (i.e. the response to the “submit” request contains the content), or if it’s going to take longer than that, the server should continue processing the request in the background and return an ID that the client can poll on later… 🧵
My current backend is JDBC so I know about wrapping code in
withContext(<http://Dispatchers.IO|Dispatchers.IO>) { }
but is there a good way to park a continuation/callback in an ExecutorService or something?


12/22/2022, 12:01 AM
Copy code
sealed interface Result {
  class OperationCompleted(val theStuff: Any)
  object TimeoutReached

// ...

val myOperation = async { doTheOperation() } 

val result = select {
  onTimeout { TimeoutReached }
  muOperation.onAwait { OperationCompleted(it) }

when (result) {
  TimeoutReached -> {
    val token = addOperationToQueue(myOperation)
    return token

  is OperationCompleted -> return result.theStuff