How can I make this code with AsyncData better? #46
-
I am reproducing James Sinclair's book A Skeptic's Guide to FP, using Ramda + Swan-io/boxed. In the example, json-server is used. If you can optimize it, feel free to use it in the documentation. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
hi! in the way the code is written, In your example, I'd go for something like the following: Future.fromPromise(axios({
method: 'GET',
baseURL: 'http://localhost:4000/notificationData',
}))
.mapOk(json => Result.fromExecution(() => Serializer.decode(json)))
.mapOk(dataForTemplate)
.onResolve(result => {
result.match({
Ok: data => {
// do something with data
},
Error: error => {
// do something with error
}
})
}) |
Beta Was this translation helpful? Give feedback.
hi!
in the way the code is written,
AsyncData
doesn't serve a real purpose because you don't have somewhere where the async status is tracked. a good use case for it is for example when using React and needing to display the various stages of loading/receiving (like in this example: https://swan-io.github.io/boxed/react-request).In your example, I'd go for something like the following: