Timer

@implicitNotFound("Cannot find an implicit value for Timer[${F}]:\n* import Timer[${F}] from your effects library\n* if using IO, use cats.effect.IOApp or build one with cats.effect.IO.timer\n") trait Timer[F[_]]

Timer is a scheduler of tasks.

Timer is a scheduler of tasks.

This is the purely functional equivalent of:

It provides:

  1. the ability to get the current time
  2. ability to delay the execution of a task with a specified time duration

It does all of that in an F monadic context that can suspend side effects and is capable of asynchronous execution (e.g. IO).

This is NOT a type class, as it does not have the coherence requirement.

Companion
object
class Object
trait Matchable
class Any

Value members

Abstract methods

def clock: Clock[F]
Implicitly added by deriveKleisli

Returns a Clock instance associated with this timer that can provide the current time and do time measurements.

Returns a Clock instance associated with this timer that can provide the current time and do time measurements.

def clock: Clock[F]

Returns a Clock instance associated with this timer that can provide the current time and do time measurements.

Returns a Clock instance associated with this timer that can provide the current time and do time measurements.

def sleep(duration: FiniteDuration): F[Unit]
Implicitly added by deriveKleisli

Creates a new task that will sleep for the given duration, emitting a tick when that time span is over.

Creates a new task that will sleep for the given duration, emitting a tick when that time span is over.

As an example on evaluation this will print "Hello!" after 3 seconds:

 import cats.effect._
 import scala.concurrent.duration._

 Timer[IO].sleep(3.seconds).flatMap { _ =>
   IO(println("Hello!"))
 }

Note that sleep is required to introduce an asynchronous boundary, even if the provided timespan is less or equal to zero.

def sleep(duration: FiniteDuration): F[Unit]

Creates a new task that will sleep for the given duration, emitting a tick when that time span is over.

Creates a new task that will sleep for the given duration, emitting a tick when that time span is over.

As an example on evaluation this will print "Hello!" after 3 seconds:

 import cats.effect._
 import scala.concurrent.duration._

 Timer[IO].sleep(3.seconds).flatMap { _ =>
   IO(println("Hello!"))
 }

Note that sleep is required to introduce an asynchronous boundary, even if the provided timespan is less or equal to zero.