Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rescriptbr/reason-async-hook
A simple hook for sane promise handling in ReasonReact
https://github.com/rescriptbr/reason-async-hook
Last synced: 3 months ago
JSON representation
A simple hook for sane promise handling in ReasonReact
- Host: GitHub
- URL: https://github.com/rescriptbr/reason-async-hook
- Owner: rescriptbr
- Archived: true
- Created: 2020-07-18T13:37:54.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-07-21T01:41:25.000Z (over 4 years ago)
- Last Synced: 2024-05-11T23:34:41.020Z (6 months ago)
- Language: Reason
- Homepage:
- Size: 157 KB
- Stars: 42
- Watchers: 4
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README-pt.md
Awesome Lists containing this project
- awesome-list - reason-async-hook
README
Installation •
Usage •
How does it work
```reason
[@react.component]
let make = () => {
let ({state: createUserState}, createUser) = AsyncHook.use((~cb, ~name, ~age) => cb(() => {
fetch("/user", ~params={
name,
age,
})
}))let onSubmit= () => {
createUser(~name=form.values.name, ~age=form.values.age)
// Handle the result after the call
|> Promise.tapError(error => {
Js.log(error)
})
|> Promise.getOk(result => {
Js.log("Success!")
})
}// or use the declarative mode
{
switch(createUserState) {
| Idle => onSubmit()}>"Create user"->React.string
| Loading =>{"Loading..."->React.string}
| Error(error) =>
"User created with id #"->React.string
{data.user.id->React.string}
| Data(data) =>
"User created with id #"->React.string
{data.user.id->React.string}
}
}
}
```# Instalação
```
yarn add reason-async-hook reason-promise
````bsconfig.json`
```json
"bs-dependencies": [
"reason-async-hook",
"reason-promise"
]
```Promises são apenas melhores com reason-promise!
# Como isso funciona?
A lib apenas usa o currying por padrão e a inferência de tipos mágica do ReasonML.
`reason-promise` roda em cima das promises JavaScript mas ele adiciona mais funcionalidades a elas, e uma delas é o suporte ao tipo `result`. Permitindo controle fino sobre o fluxo da chamada e permitindo _railway programming_.