Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run two commands at the same time in Elm

Tags:

javascript

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.

like image 852
antfx Avatar asked Aug 20 '16 09:08

antfx


People also ask

What is CMD in Elm?

Commands. type Cmd msg. A command is a way of telling Elm, “Hey, I want you to do this thing!” So if you want to send an HTTP request, you would need to command Elm to do it. Or if you wanted to ask for geolocation, you would need to command Elm to go get it.


1 Answers

Use Platform.Cmd.batch (docs):

init : (Model, Cmd Msg) init =   ( Model "" []   , Cmd.batch [fetchCmd, Ports.messageToJs "Hello JS")]   ) 
like image 102
Søren Debois Avatar answered Sep 19 '22 12:09

Søren Debois