Sabha B
Sabha B

Reputation: 2089

Elm Http example Http.send call

I have gone through the documentation of each of the Http functions (arguments and return values) , with little knowledge in function currying I could not understand the function call order from Http.send written in getRandomGif : String -> Cmd Msg function

Need help in understanding/Expanding the Http.send , order in which the function are called. http://elm-lang.org/examples/http

-- send : (Result Error a -> msg) -> Request a -> Cmd msg
-- NewGif (Result Http.Error String)
-- get : String -> Decoder a -> Request a

Http.send NewGif (Http.get url decodeGifUrl)

Upvotes: 2

Views: 1886

Answers (1)

Sabha B
Sabha B

Reputation: 2089

Http.send internally calls Task

Here are the Response from other channels (Slack & Youtube)

https://www.youtube.com/watch?v=EDp6UmaA9CM

Elm's task system will eventually invoke the constructor function LoadUser.

Http.send creates a Cmd that we hand to the Elm architecture so it can perform the HTTP request on our behalf. The function we provide to Http.send informs Elm how we want to handle the result when it comes back.

You can see where the Cmd is created here: https://github.com/jfairbank/arch-elm/blob/master/app/src/Profile.elm#L120-L121.

Because we provide a Msg value LoadUser as our function to Http.send, it allows us to respond to LoadUser later in our update function once we have the result back.

Here is where Elm generates a Task: https://github.com/elm-lang/http/blob/master/src/Http.elm#L85-L87. The resultToMessage parameter would be LoadUser in this case.

It might be hard to understand with the function composition operator, but here is where eventually resultToMessage, or LoadUser in this case, gets called in the Task module: https://github.com/elm-lang/core/blob/5.1.1/src/Task.elm#L237-L243.

https://spin.atomicobject.com/2016/10/11/elm-chain-http-requests/

Upvotes: 2

Related Questions