Mike Speed
09/24/2021, 1:22 PMprivate suspend fun getThing(){...}
How can I set a minimum return time on this? So it will only return if at least 2000ms have passed, for example?Paul Woitaschek
09/24/2021, 1:23 PMMike Speed
09/24/2021, 1:24 PMdelay(2000)
?Paul Woitaschek
09/24/2021, 1:26 PMsuspend fun getThing() {
val start = System.currentTimeMillis()
doStuff()
val elapsed = System.currentTimeMillis() - start
delay(2000 - elapsed)
}
Mike Speed
09/24/2021, 1:26 PMChris Miller
09/24/2021, 1:29 PMdelay(2000 - measureTimeMillis { doStuff() })
Paul Woitaschek
09/24/2021, 1:29 PMsuspend fun getThing() : Int {
return iWillReturnEarliestInTwoSeconds {
doStuff()
}
}
suspend inline fun <T> iWillReturnEarliestInTwoSeconds(action: () -> T): T {
val start = System.currentTimeMillis()
val result = action()
val elapsed = System.currentTimeMillis() - start
delay(2000 - elapsed)
return result
}
Mike Speed
09/24/2021, 1:32 PMmeasureTimeMillis{}
is basically doing what you have written here @Paul Woitaschek?Chris Miller
09/24/2021, 1:36 PMMike Speed
09/24/2021, 1:36 PMPaul Woitaschek
09/24/2021, 1:39 PMMike Speed
09/24/2021, 1:40 PMJoffrey
09/24/2021, 2:03 PMPaul Woitaschek
09/24/2021, 2:15 PMsuspend inline fun <T> iWillReturnEarliestInTwoSeconds(action: () -> T): T {
val (result, elapsed) = measureTimedValue(action)
delay(Duration.seconds(2) - elapsed)
return result
}
suspend inline fun <T> iWillReturnEarliestInTwoSeconds(action: () -> T): T = measureTimedValue(action)
.also {
delay(Duration.seconds(2) - it.duration)
}.value
ephemient
09/24/2021, 5:15 PMprivate suspend fun getThing() {
launch { delay(2000) }
// do other stuff
uli
09/24/2021, 5:38 PMprivate suspend fun getThing() = coroutineScooe {
launch { delay(2000) }
// do other stuff
}
Paul Woitaschek
09/24/2021, 6:17 PMuli
09/24/2021, 6:33 PMprivate suspend fun getThing() {
val delayJob = launch { delay(2000) }
// do other stuff
delayJob.join()