fabulous-dev / Fabulous

Declarative UI framework for cross-platform mobile & desktop apps, using MVU and F# functional programming
https://fabulous.dev
Apache License 2.0
1.13k stars 122 forks source link

Add helper function Cmd.ofAsyncResult #931

Closed TimLariviere closed 2 years ago

TimLariviere commented 2 years ago

Cmd.ofAsyncResult is useful when dealing with API calls

module Server =
    let getDataAsync () =
        async {
                let! response = ...

                if response.StatusCode = Unauthorized then
                     return Error "Unauthorized"
                else
                     return Ok response.Data
        }

Cmd.ofAsyncResult
     Server.getDataAsync
     (fun data -> DataRetrievedSuccessfully data)
     (fun error -> DataRetrievalError error)
     (fun ex -> DataRetrievalFailure ex)