https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

Matej Kormuth

03/02/2019, 3:14 PM
Hi, I launch a coroutine with
while(true) { ... delay(3000) }
code in it. Is is possible to somehow trigger skip of the delay (waiting 3s) and run the while loop body immediately?
d

Dominaezzz

03/02/2019, 3:41 PM
Where do you want to trigger the skip from?
By skip, I assume you mean to interrupt the coroutine suspended in the
delay
function.
select
might help you.
e

elizarov

03/02/2019, 3:53 PM
Yes. Create a channel, and the instead of
delay(3000)
, do:
Copy code
select { 
    channel.onReceive { /* skipped */ }
    onTimeout(3000) { /* waited */ }
}
1
👍🏼 1
2 Views