# JavaScript can do an http request with closure, but how does that work in Elm?

**URL:** <https://discourse.elm-lang.org/t/javascript-can-do-an-http-request-with-closure-but-how-does-that-work-in-elm/1565>\
**Category:** Learn\
**Created:** [July 26, 2018, 10:58pm UTC](https://discourse.elm-lang.org/t/javascript-can-do-an-http-request-with-closure-but-how-does-that-work-in-elm/1565 "2018-07-26T22:58:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![berend](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/berend/32/2250_2.png) [@berend](https://discourse.elm-lang.org/u/berend)\
**Post date:** [July 26, 2018, 10:58pm UTC](https://discourse.elm-lang.org/t/javascript-can-do-an-http-request-with-closure-but-how-does-that-work-in-elm/1565/1 "2018-07-26T22:58:12Z")

</div>

In JavaScript it’s pretty common to use closures, very handy for example when making http requests, and remembering what you asked for. I.e.

```
var id = 3;
http_request (function (result) {
  // I know I made the request for id 3, 
  // even though the result variable may not mention this
});

```

How do you do this in Elm? It seems that in Elm the result needs to tell my program what it was for.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![berend](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/berend/32/2250_2.png) [@berend](https://discourse.elm-lang.org/u/berend)\
**Post date:** [July 26, 2018, 11:09pm UTC](https://discourse.elm-lang.org/t/javascript-can-do-an-http-request-with-closure-but-how-does-that-work-in-elm/1565/2 "2018-07-26T23:09:23Z")

</div>

Need more coffee in the morning 🙂 But as usual. phrasing the question is answering it, so may have helped me.

This is how to do it obviously:

`Http.send (Foo id) request`

---

<div class="post-metadata">

**Author:** ![G4BB3R](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/g4bb3r/32/4544_2.png) [@G4BB3R](https://discourse.elm-lang.org/u/G4BB3R)\
**Post date:** [July 27, 2018, 1:16am UTC](https://discourse.elm-lang.org/t/javascript-can-do-an-http-request-with-closure-but-how-does-that-work-in-elm/1565/3 "2018-07-27T01:16:18Z")

</div>

What’s foo msg signature and request implementation? I need more details.

---

<div class="post-metadata">

**Author:** ![berend](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/berend/32/2250_2.png) [@berend](https://discourse.elm-lang.org/u/berend)\
**Post date:** [July 27, 2018, 1:47am UTC](https://discourse.elm-lang.org/t/javascript-can-do-an-http-request-with-closure-but-how-does-that-work-in-elm/1565/4 "2018-07-27T01:47:54Z")

</div>

```
request = 
    Http.get "/foo" decodeResponse

Msg =
    Foo Int (Result Http.Error ValidResponse)

```

In case it isn’t clear, I answered my own question.

---

<div class="post-metadata">

**Author:** ![G4BB3R](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/g4bb3r/32/4544_2.png) [@G4BB3R](https://discourse.elm-lang.org/u/G4BB3R)\
**Post date:** [July 27, 2018, 2:03am UTC](https://discourse.elm-lang.org/t/javascript-can-do-an-http-request-with-closure-but-how-does-that-work-in-elm/1565/5 "2018-07-27T02:03:07Z")

</div>

> request : Http.Request ValidResponse  
> request = Http.get “/foo” decodeResponse
> 
> type Msg = Foo Int (Result Http.Error ValidResponse)

Now I noticed that you already solved your problem. This is a nice way, but if I would need this information later, I would store in the model instead of passing in the first parameter. But you did right for your case

---

<div class="post-metadata">

**Author:** ![berend](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/berend/32/2250_2.png) [@berend](https://discourse.elm-lang.org/u/berend)\
**Post date:** [July 27, 2018, 7:32am UTC](https://discourse.elm-lang.org/t/javascript-can-do-an-http-request-with-closure-but-how-does-that-work-in-elm/1565/6 "2018-07-27T07:32:19Z")

</div>

> [@G4BB3R](#):
>
> This is a nice way, but if I would need this information later, I would store in the model instead of passing in the first parameter. But you did right for your case

The problem is that this does not work: http requests are asynchronous, so you cannot control when a response is received, so you cannot match a state in your model against a randomly incoming response. It might be a delayed and stale response.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [August 6, 2018, 7:32am UTC](https://discourse.elm-lang.org/t/javascript-can-do-an-http-request-with-closure-but-how-does-that-work-in-elm/1565/7 "2018-08-06T07:32:20Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
