How can I create a timer? I've done it like this `...
# coroutines
p
How can I create a timer? I've done it like this
Copy code
scope.launch{
 var foo = true
 while(foo){
 delay(1_000)
     //do some actions here
     //with these actions I check if the time is correct and if goes inside the if i put the foo to false so it leaves the while
     if(whatever) {
       foo=false
       //call one method here
     }
 }
}
Is this a correct way? Is there a better way with flow?
d
If you're specifically looking for a way to do this as a flow, something like this would probably work:
Copy code
flow {
  while (true) {
    emit(someValue)
    delay(1000)
  }
}.takeWhile { whatever }
p
I mean the way I did it correct without flow?
i'd like to avoid this while
g
why do you want to avoid
while
?
p
I mean I'd like to listen another options, is this an optimal solution?
is flow doing some better things behind to replace this method I wrote it down?
g
yes, it’s perfectly fine, I don’t see what makes you search for another solution
u
Is it a one shot timer and the idea is to poll every second, then fire once and exit? If so, why don't you set the delay to that time and get rid of the loop?