Search code examples
elm

Elm 0.16 to 0.18 Http.get?


I've been trying to convert my Elm 0.16 code to 0.18. However I can't seem to get the Http.get I had to work.

The old code was:

fetch : Decoder a -> String -> (Result Http.Error a -> b) -> Effects b --Effects became Cmd in 0.17
fetch decoder url action =
    Http.get decoder url
    |> Task.toResult
    |> Task.map action
    |> Effects.task

But Task.toResult doesn't exist anymore. I found a google groups conversation that stated I had to convert

task |> Task.toResult |> Task.map action |> Effects.task to

task |> Task.toResult |> Task.perform never action

But then I still get the "Task does not expose toResult" error since in 0.18 they removed that it seems.

How can I fix this? I've tried searching the web, but find everything very confusing and not very useful.


Solution

  • You only need to use Http.get and Http.send for your fetch function:

    fetch : Decoder a -> String -> (Result Http.Error a -> b) -> Cmd b
    fetch decoder url action =
        Http.get url decoder
            |> Http.send action
    

    Since 0.16, the Http package does not require you to use an intermediate Task before sending the request. You can still convert a request to a Task if needed by using Http.toTask, in case you want to chain together multiple requests.