Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions core-tests/shared/src/test/scala/zio/ZIOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3158,6 +3158,30 @@ object ZIOSpec extends ZIOBaseSpec {
assert(effect)(equalTo(42))
}
),
suite("tapEither")(
testM("effectually peeks at the failure of this effect") {
for {
ref <- Ref.make(0)
_ <- IO.fail(42)
.tapEither {
case Left(value) => ref.set(value)
case Right(_) => ref.set(-1)
}
.run
effect <- ref.get
} yield assert(effect)(equalTo(42))
},
testM("effectually peeks at the success of this effect") {
for {
ref <- Ref.make(0)
_ <- Task(42).tapEither {
case Left(_) => ref.set(-1)
case Right(value) => ref.set(value)
}.run
effect <- ref.get
} yield assert(effect)(equalTo(42))
}
),
suite("tapCause")(
testM("effectually peeks at the cause of the failure of this effect") {
for {
Expand Down
11 changes: 11 additions & 0 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,17 @@ sealed trait ZIO[-R, +E, +A] extends Serializable with ZIOPlatformSpecific[R, E,
): ZIO[R1, E1, A] =
self.foldCauseM(new ZIO.TapErrorRefailFn(f), new ZIO.TapFn(g))

/**
* Returns an effect that effectfully "peeks" at the result of this effect.
* {{{
* readFile("data.json").tapEither(result => log(result.fold("Error: " + _, "Success: " + _)))
* }}}
*/
final def tapEither[R1 <: R, E1 >: E](f: Either[E, A] => ZIO[R1, E1, Any])(implicit
ev: CanFail[E]
): ZIO[R1, E1, A] =
self.foldCauseM(new ZIO.TapErrorRefailFn(e => f(Left(e))), new ZIO.TapFn(a => f(Right(a))))

/**
* Returns an effect that effectually "peeks" at the cause of the failure of
* this effect.
Expand Down