'Run two commands at the same time in Elm

In Elm, and specifically with the Elm Architecture when the app first starts the init function can return a Cmd Msg that is executed. We can use this for sending http requests or send a message to native Javascript via Elm ports.

My question is, how can I send multiple commands that should be executed in init?

For example I can do something like:

init : (Model, Cmd Msg)
init =
  (Model "" [], (Ports.messageToJs "Hello JS"))

And I can do something like:

url : String
url =
     "http://some-api-url.com"
...

fetchCmd : Cmd Msg
fetchCmd =
    Task.perform FetchError FetchSuccess fetchTask


init : (Model, Cmd Msg)
init =
  (Model "" [], fetchCmd)

How can I return both commands at same time from init?

I have seen Task.sequence and even Task.parallel but they appear to be good for running multiple tasks, not specifically commands.



Solution 1:[1]

Do as Sören says, or use the newer, equivalent "bang" -syntax :

init : (Model, Cmd Msg)
init =
  ( Model "" [] )
  ! [fetchCmd, Ports.messageToJs "Hello JS"]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 swelet