Justin Tullgren
11/16/2021, 4:32 PMJustin Tullgren
11/16/2021, 4:32 PMJoffrey
11/16/2021, 4:34 PMval deferred = TODO("whatever gives you the deferred, maybe an async{} call")
val value = withTimeoutOrNull(1000) { deferred.await() }
if (value == null) {
// handle the timeout
} else {
// use the deferred value
}
Justin Tullgren
11/16/2021, 4:34 PMtry {
withTimeout(5000) {
deferred.await()
}
catch(t: TimeoutException) {
emit(TimedOut) // let people know to use cache
val finishedResult = deferred.await()
emit(finishedResult)
}
Justin Tullgren
11/16/2021, 4:36 PMwithTimeoutOrNull
cancel the deferred though? Thats what i am getting as an exception, that I am trying to do something in a cancelled context violating exception transparencyJoffrey
11/16/2021, 4:39 PM{ ... }
(so the await
call in this case), but the deferred itself is controlled by whatever scope you ran it with:
https://pl.kotl.in/AW8gJcbu_Justin Tullgren
11/16/2021, 4:40 PMJoffrey
11/16/2021, 4:41 PMJustin Tullgren
11/16/2021, 4:42 PM