Last active
May 25, 2024 10:21
-
-
Save dacr/c597466163c75e9150fb9a910d9693ca to your computer and use it in GitHub Desktop.
ZIO learning - promise allow one fiber to communicate with an other one / published by https://github.com/dacr/code-examples-manager #1a10f606-24f5-45a1-9577-dad815cd2146/f0234d16a783ec0acd7948bfdc130fc0f174270f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : ZIO learning - promise allow one fiber to communicate with an other one | |
// keywords : scala, zio, learning, pure-functional, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 1a10f606-24f5-45a1-9577-dad815cd2146 | |
// created-on : 2021-04-06T15:32:12+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "dev.zio::zio:2.0.13" | |
// --------------------- | |
/* | |
inspired from | |
- [John A De Goes - ZIO: Next-Generation Effects in Scala](https://www.youtube.com/watch?v=mkSHhsJXjdc) | |
- +35:00 | |
*/ | |
import zio.* | |
object Encapsulated extends ZIOAppDefault { | |
def completeWhenReady[E](promise: Promise[E, Int]) = { | |
Clock.sleep(100.millis).flatMap { case x => | |
promise.complete(ZIO.succeed(42)) | |
} | |
} | |
def run = for { | |
p <- Promise.make[Error, Int] | |
_ <- completeWhenReady(p).fork | |
_ <- Clock.sleep(50.millis) // do so work | |
result <- p.await | |
_ <- Console.printLine(s"result=$result") | |
} yield result | |
} | |
// ------------------------------------------------------------- | |
Encapsulated.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment