Skip to content

Commit

Permalink
Merge pull request #327 from d1skort/timeout-docs
Browse files Browse the repository at this point in the history
add timeout docs
  • Loading branch information
Odomontois authored Jul 2, 2020
2 parents 4ea86cb + 9d2c03d commit 6c65730
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/timeout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
id: timeout
title: Timeout
---

A simple typeclass, which allows you to timeout any process.

```scala mdoc
import cats.Monad
import cats.effect.Timer
import monix.eval.Task
import monix.execution.Scheduler.Implicits.global

import tofu.{ Timeout, Raise }
import tofu.syntax.monadic._

import scala.concurrent.duration._


def computeValue[F[_]: Monad](implicit timer: Timer[F]): F[String] =
timer.sleep(3.seconds) >> "value".pure[F]

def program[F[_]: Monad: Timeout: Timer]: F[String] =
Timeout[F].timeoutTo(computeValue, 1.seconds, "fallback".pure[F])

program[Task].runSyncUnsafe(Duration.Inf)
```

Timeout comes with handy syntax:

```scala mdoc
import tofu.syntax.timeout._

def program1[F[_]: Monad: Timeout: Timer]: F[Option[String]] =
computeValue.timeout(1.seconds)

def program2[F[_]: Monad: Timeout: Timer]: F[String] =
computeValue.timeoutOr(1.seconds, "fallback")

def program3[F[_]: Monad: Raise[*[_], Exception]: Timeout: Timer]: F[String] =
computeValue.timeoutRaise(1.seconds, new Exception("boom"))

program1[Task].runSyncUnsafe(Duration.Inf)

program2[Task].runSyncUnsafe(Duration.Inf)

program3[Task].onErrorHandle(_ => "fallback").runSyncUnsafe(Duration.Inf)
```
1 change: 1 addition & 0 deletions website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"ids": [
"errors",
"fork",
"timeout",
"typeclasses"
]
},
Expand Down

0 comments on commit 6c65730

Please sign in to comment.