# Wait before all Cmds return before rendering the next view

**URL:** <https://discourse.elm-lang.org/t/wait-before-all-cmds-return-before-rendering-the-next-view/2206>\
**Category:** Learn\
**Created:** [October 14, 2018, 9:33am UTC](https://discourse.elm-lang.org/t/wait-before-all-cmds-return-before-rendering-the-next-view/2206 "2018-10-14T09:33:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sashaafm](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/sashaafm/32/453_2.png) [@sashaafm](https://discourse.elm-lang.org/u/sashaafm)\
**Post date:** [October 14, 2018, 9:33am UTC](https://discourse.elm-lang.org/t/wait-before-all-cmds-return-before-rendering-the-next-view/2206/1 "2018-10-14T09:33:52Z")

</div>

When Elm processes the `update` function it return the updated `Model` and a list of `Cmd`s to execute. AFAIK, these `Cmd`s run asynchronously, meaning the updated `Model` is sent to the `view` function to be rendered while the `Cmd`s are still executing. Then, when these `Cmd`s return, the `Model` is updated once again for each message received from the `Cmd`s and the view is updated.

I’m thinking if it is possible to somehow wait for all the `Cmd`s to return, before rendering the next view? A possible use case for this would be to send an HTTP request to the server, and wait for it return either a 200 OK or a 404 Not Found, and this would be used to decide which view would be presented (successful presentation of the resource or a generic Not Found page).

Another possible use case would be to prevent “loading” screens or spinners across the view and just show complete views.

Thanks!

---

<div class="post-metadata">

**Author:** ![norpan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/norpan/32/548_2.png) [@norpan](https://discourse.elm-lang.org/u/norpan)\
**Post date:** [October 14, 2018, 12:42pm UTC](https://discourse.elm-lang.org/t/wait-before-all-cmds-return-before-rendering-the-next-view/2206/2 "2018-10-14T12:42:26Z")

</div>

Not updating the view would mean that the whole app freezes until you get the reply. I don’t think you want this. But, you can certainly make it so that you have a custom datatype that collects all the replies and only when all replies have come back do you change what is displayed in your view.

---

<div class="post-metadata">

**Author:** ![sashaafm](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/sashaafm/32/453_2.png) [@sashaafm](https://discourse.elm-lang.org/u/sashaafm)\
**Post date:** [October 14, 2018, 1:35pm UTC](https://discourse.elm-lang.org/t/wait-before-all-cmds-return-before-rendering-the-next-view/2206/3 "2018-10-14T13:35:28Z")

</div>

Yes, that’s exactly what I was thinking would happen. This could be mitigated with an intermediate “Loading” bar on top of the current view, but I understand it kind of defeats the purpose.

---

<div class="post-metadata">

**Author:** ![dillonkearns](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dillonkearns/32/1587_2.png) [@dillonkearns](https://discourse.elm-lang.org/u/dillonkearns)\
**Post date:** [October 15, 2018, 5:35am UTC](https://discourse.elm-lang.org/t/wait-before-all-cmds-return-before-rendering-the-next-view/2206/4 "2018-10-15T05:35:51Z")

</div>

Take a look at the [remote-data](https://package.elm-lang.org/packages/krisajenkins/remotedata/latest) package, I think this will do exactly what you’re looking for. This is really commonly used. It’s very simple but powerful. You can even take several http requests and merge it into a single data type, see [its mapN functions](https://package.elm-lang.org/packages/krisajenkins/remotedata/latest/RemoteData#map3). Using the mapN functions you, you can show a loading spinner if _any_ of the requests are loading, or an error message if _any_ of the requests have an error. Pretty handy!

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [October 15, 2018, 9:35am UTC](https://discourse.elm-lang.org/t/wait-before-all-cmds-return-before-rendering-the-next-view/2206/5 "2018-10-15T09:35:30Z")

</div>

> [@sashaafm](#):
>
> Then, when these `Cmd` s return, the `Model` is updated once again for each message received from the `Cmd` s and the view is updated.

Be aware that an Elm application does not necessarily proceed like this: `update -> view -> update -> view -> update -> view -> ...`, that is, 1 `update` and 1 `view` interleaved. As the `Cmd`s are asynchronous and the `view` function is only called on the animation frame, there can be many updates per view. This asynchronousity is necessary to deal with the user interaction and other system (HTTP requests, timer, keyboard, mouse etc) - and for that reason you would not want to block on either the `update` or the `view` functions.

---

<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:** [October 25, 2018, 9:35am UTC](https://discourse.elm-lang.org/t/wait-before-all-cmds-return-before-rendering-the-next-view/2206/6 "2018-10-25T09:35:35Z")

</div>

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