Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jderochervlk/rescript-future
Functional Futures for ReScript
https://github.com/jderochervlk/rescript-future
Last synced: 9 days ago
JSON representation
Functional Futures for ReScript
- Host: GitHub
- URL: https://github.com/jderochervlk/rescript-future
- Owner: jderochervlk
- Created: 2024-02-12T16:24:32.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-02-15T00:27:34.000Z (10 months ago)
- Last Synced: 2024-03-15T00:51:31.073Z (9 months ago)
- Language: JavaScript
- Size: 20.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @jvlk/rescript-future
## Installation```sh
npm install @vlk/rescript-future
```Update `rescript.json`.
```json
{
"bs-dependencies": [
"@jvlk/rescript-future"
]
}
```## Basic Example
```rescript
let _ =
Future.fetch("http://httpstat.us/200")
->Future.flatMap(res => {
switch res->Fetch.Response.ok {
| true => Ok(res)
| false =>
Error(`${res->Fetch.Response.status->Int.toString}: ${res->Fetch.Response.statusText}`)
}
})
->Future.map(res => res->Fetch.Response.statusText)
->Future.fold(Console.error, Console.log)
```
More examples can be found in the `examples` directory.### React
```rescript
@react.component
let make = () => {
let request = Future.fetch("http://httpstat.us/200?sleep=1000")
{
request->Future.reset // reset the future to make sure the Abort Controller is new
Console.log("clicked!")
let _ = request->Future.fold(Console.error, Console.log)
}}>
{"Start"->React.string}
{
let _ = request->Future.cancel // Aborts the fetch request and stops any processing of the response
}}>
{"Cancel"->React.string}
}
```