Cmd.batch
[ Task.perform (always (AddString "first")) (Task.succeed ())
, Task.perform (always (AddString "second")) (Task.succeed ())
, Task.perform (always (AddString "third")) (Task.succeed ())
]third
second
first--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
The execution of a Cmd.batch list of commands has not ordering guarantee.
What you see is an artifact and you should not treat it as a predictable way of execution.
The way to think about a Cmd.batch is that they will get executed and you will eventually get a reply.
If you need a list of tasks executed in order, you need to implement what you mean by that yourself.For example, Task.sequence executes all tasks in order and if one fails, the entire execution fails.
Task.andThen is another way to chain execution where you have access to the result of the previous execution.
It depends on what you want to happen.
On Fri, May 12, 2017 at 6:36 PM, Frank Bonetti <frank.r...@gmail.com> wrote:
If you batch a list of commands, they will be executed in reverse order. For example:Cmd.batch
[ Task.perform (always (AddString "first")) (Task.succeed ())
, Task.perform (always (AddString "second")) (Task.succeed ())
, Task.perform (always (AddString "third")) (Task.succeed ())
]Will print:third
second
firstHas anyone else noticed this behavior? If so, do you know why Cmd.batch was implemented this way?Here's a demo:
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Here are two approaches to just adding the texts (I'm assuming some interface where you want to send a series of messages one after the other and having the user seeing them arrive one after the other with some delay between them)
On Fri, May 12, 2017 at 6:36 PM, Frank Bonetti <frank.r...@gmail.com> wrote:
If you batch a list of commands, they will be executed in reverse order. For example:Cmd.batch
[ Task.perform (always (AddString "first")) (Task.succeed ())
, Task.perform (always (AddString "second")) (Task.succeed ())
, Task.perform (always (AddString "third")) (Task.succeed ())
]Will print:third
second
firstHas anyone else noticed this behavior? If so, do you know why Cmd.batch was implemented this way?Here's a demo:
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Here are two approaches to just adding the texts (I'm assuming some interface where you want to send a series of messages one after the other and having the user seeing them arrive one after the other with some delay between them)
On Fri, May 12, 2017 at 6:36 PM, Frank Bonetti <frank.r...@gmail.com> wrote:
If you batch a list of commands, they will be executed in reverse order. For example:Cmd.batch
[ Task.perform (always (AddString "first")) (Task.succeed ())
, Task.perform (always (AddString "second")) (Task.succeed ())
, Task.perform (always (AddString "third")) (Task.succeed ())
]Will print:third
second
firstHas anyone else noticed this behavior? If so, do you know why Cmd.batch was implemented this way?Here's a demo:
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Also, to the best of my knowledge, the only way to execute Tasks concurrently and independently is to use Cmd.batch.
This is a contradiction. Even if the runtime started the tasks at the same time, the works they do can complete in any order. Either your code (Elm or otherwise) should work with any ordering, or you should sequence things in Elm tasks.
If the runtime is iterating through the list backwards, that's an implementation detail.
This is a contradiction. Even if the runtime started the tasks at the same time, the works they do can complete in any order. Either your code (Elm or otherwise) should work with any ordering, or you should sequence things in Elm tasks.
I'm well aware that the tasks can complete in an order. I was just wondering why the commands are started in reverse order.
Set.fromList [4,2,5,3,1] |> Set.toList == [1,2,3,4,5]> Imagine for a second that the first task takes 2 seconds the second task 1 second and the third 500ms. They finish in reverse order. If you want their result immediately after they finish, the results will come in the wrong order.Totally understand. Why do the commands start in reverse order though?
cmd =
Cmd.batch
[ getReallyImportantPieceOfData
, getLessImportantThing1
, getLessImportantThing2
, getLessImportantThing3
, getLessImportantThing4
, getLessImportantThing5
, getLessImportantThing6
, getLessImportantThing7
, getLessImportantThing8
, getLessImportantThing9
]/getLessImportantThing9.json
/getLessImportantThing8.json
/getLessImportantThing7.json
/getLessImportantThing6.json
/getLessImportantThing5.json
/getLessImportantThing4.json
/getLessImportantThing3.json
/getLessImportantThing2.json
/getLessImportantThing1.json
/getReallyImportantPieceOfData.jsonTo give you some context, I'm working on a single page app:1. When the user loads a particular page, the app makes 10 independent ajax requests to get various pieces of data. Some of these requests take a while to complete while others complete instantaneously.2. In general, it doesn't matter when these requests start and end, but there is one particular request which needs to be executed as quickly as possible in order to make the page feel snappy.3. Since the browser can only execute a limited number of ajax requests in parallel, the order of when the requests start is critical. If my most important request is put last in the queue, it won't be executed until the AJAX thread pool frees up a spot, which could take a while, resulting in a page that feels slow.
The execution of a Cmd.batch list of commands has not ordering guarantee.
type Msg =Later (Cmd Msg)update : Msg -> model -> ( model, Cmd Msg )update msg model =case msg ofLater cmd -> ( model, cmd )later : Cmd Msg -> Cmd Msglater cmd =Task.succeed cmd|> Task.perform Later