elm-spa-example icon indicating copy to clipboard operation
elm-spa-example copied to clipboard

Task.map causing HTTP requests do be done in sequence

Open dalen opened this issue 8 years ago • 5 comments

Thanks for the nice example, I'm borrowing some ideas for my open source Elm SPA I'm developing.

But I'm wondering about the use of Task,map instead of Cmd.batch, it seems that causes all requests to be done in sequence instead of in parallel. For example the list of articles and the list of tags are loaded after each other instead of in parallel. Is there any way to fix that?

dalen avatar May 16 '17 13:05 dalen

@dalen can you provide a link to the actual use of Task.map that is worrying you?

I cannot find the actual place that does what you describe.

As far as I can see, all the requests needed on page load are joined together into a single task using Task.map2.

pdamoc avatar Jun 19 '17 11:06 pdamoc

@pdamoc The problem is that Task.map2 runs the tasks in sequence instead of in parallel: http://package.elm-lang.org/packages/elm-lang/core/latest/Task#map2

Put the results of two tasks together. If either task fails, the whole thing fails. It also runs in order so the first task will be completely finished before the second task starts.

dalen avatar Jun 20 '17 09:06 dalen

It's a good question. Looks like that would be kind of a big change, since the convention here is each page init returns a single Task that then gets attempted to a single Msg callback in Main: https://github.com/rtfeldman/elm-spa-example/blob/master/src/Main.elm#L245 .

You could instead have a more "conventional" setup with page init returning (model, Cmd msg) and do a Cmd.map in Main, but then you end up pushing down handling http results to the page, when you really need to deal with them in Main in order to set pageState. I can think of a few ways of doing it but they all seem a bit convoluted.

ericgj avatar Jun 20 '17 16:06 ericgj

If these commands change the model state, then you need to run them sequentially. I had an issue where 2 Cmds were processing and the model of the last one to complete wins.

Hermanverschooten avatar Feb 26 '18 16:02 Hermanverschooten

@Hermanverschooten I'm not sure exactly what the Elm API for it would be, but you could have something similar to Promise.all in JavaScript to wait for all the tasks to be completed, but without doing them sequentially.

dalen avatar Apr 18 '18 09:04 dalen