How would you call a function if and only if a cor...
# coroutines
z
How would you call a function if and only if a coroutine is suspended for more than X ms?
Copy code
log.verbose("awaiting for ${request.uri}")
hasTokenStates
    .first { hasToken -> hasToken } // suspending
log.verbose("awaiting complete for ${request.uri}")
I want to only log
"awaiting for ${request.uri}"
if the
first()
suspends for more than `1_000`ms
z
Copy code
try {
  withTimeout(duration) {
    job.join()
  }
} catch (e: TimeoutException) {
  // …
}
z
interesting
I think this maybe:
Copy code
coroutineScope { // create scope from suspending context
    val logJob = launch {
        delay(1_000L)
        // only log if we have to wait for over a second
        log.verbose("API call for ${request.uri} is queued")
    }
    hasTokenStates
        .first { hasToken -> hasToken } // suspending
    logJob.cancel()
}
assuming that the
delay(1_000L)
checks for cancellation upon exiting the delay