time-out-0.2: Timers, timeouts, alarms, monadic wrappers

Safe HaskellNone
LanguageHaskell2010

Control.Timeout

Synopsis

Documentation

timeout :: (TimeUnit t, MonadIO m, MonadCatch m) => t -> m a -> m (Maybe a) Source

Run a monadic action with a time limit. If it finishes before that time passes and returns value x, then Just x is returned. If the timeout passes, the action is aborted and Nothing is returned. If the action throws an exception, it is aborted and the exception is rethrown.

>>> timeout (3 :: Second) $ delay (1 :: Second) >> return "hello"
Just "hello"
>>> timeout (3 :: Second) $ delay (5 :: Second) >> return "hello"
Nothing
>>> timeout (1 :: Second) $ error "hello"
*** Exception: hello

delay :: (TimeUnit t, MonadIO m) => t -> m () Source

Suspend the current thread for the given amount of time.

Example:

delay (5 :: Second)