Skip to content

Instantly share code, notes, and snippets.

@chenharryhua
Created May 15, 2022 22:03
Show Gist options
  • Save chenharryhua/45daa489bd856ff3c3e27492be48f1e2 to your computer and use it in GitHub Desktop.
Save chenharryhua/45daa489bd856ff3c3e27492be48f1e2 to your computer and use it in GitHub Desktop.
import fs2.Stream
val r1 = Stream.resource(Resource.make(IO.println("acquire 1"))(_ => IO.println("release 1")))
val r2 = Stream.bracket(IO.println("acquire 2"))(_ => IO.println("release 2"))
val ss =
r1.onFinalizeWeak(IO.println("final 1"))
.flatMap(_ => r2.onFinalize(IO.println("final 2"))).evalMap(_ => IO.println("do work"))
++ fs2.Stream.eval(IO.println("----")) ++ fs2.Stream.sleep[IO](2.second)
ss.compile.drain.unsafeRunSync()
@chenharryhua
Copy link
Author

chenharryhua commented May 15, 2022

acquire 1
acquire 2
doing work
release 2
final 2
release 1
----
final 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment