Recipe: Evaluate a Task Periodically

In the above section where we needed to generate an auth token every so often, we used:

val authStream: Stream[Task, AuthToken] = time.awakeEvery[Task](60.minutes).
  evalMap { _ => Task.delay(AuthToken((math.random * 1000).toInt)) }

You can also do something like:

(eval(authCreationTask).repeat zip time.awakeEvery(1.minute)).map(_._1)

which essentially does the same thing. You'll need a Scheduler and Strategy (or an Async) in implicit scope.

Last updated