Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/christopherdavenport/resource-monad-ts
https://github.com/christopherdavenport/resource-monad-ts
Last synced: 23 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/christopherdavenport/resource-monad-ts
- Owner: ChristopherDavenport
- License: mit
- Created: 2021-10-31T04:06:51.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-31T19:35:38.000Z (about 3 years ago)
- Last Synced: 2024-10-04T19:07:24.870Z (about 1 month ago)
- Language: TypeScript
- Size: 33.2 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Resource-Monad
Resource Monad is a simple implementation of a monadic style to resource handling.
```ts
import resource from "resource-monad"const log: (s: string) => resource.Resource = (s: string) => {
return resource.make(
async () => {console.log("Starting " + s)},
async () => { console.log("Shutdown " + s)}
)
}const display = log("1")
.flatMap((_) => log("2"))
.use(async (a: void) => { console.log("Using")})display()
// Starting 1
// Starting 2
// Using
// Shutdown 2
// Shutdown 1
```As you can see from the above. First resource are used, then using acts as an anchor point, and finally resources are cleaned up in the reverse order that they are required.