Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XY prob: How do I issue a bunch of msgs simultaneously?

Tags:

elm

Almost definitely an XY problem but I can't think of a concise way to phrase what I'm trying to do.

I have a textarea. Inside this text area, the user enters a comma-separated list of ID numbers. When they click "Fetch", I split their input on commas, to get multiple string values, and each of those string values gets passed to a function that makes a HTTP request to my API getting info on the item.

That's where I stumble. Right now I have these parts:

  • getInfo : String -> Cmd Msg, takes an ID string and ultimately fires Http.send
  • type Msg = Fetch String, where Fetch idStr -> (model, getInfo idStr)

I sort of want to take my textarea's input and say String.split "," |> List.map (\id -> getInfo id). Except I don't know what to do with the List Msg that would give me, I want to fire each of those msgs off, but Elm doesn't work that way?

While reading I found Cmd.batch, but there's not really any info on it in the docs so I'm not sure if that's what I want or how to use it.

like image 290
GreenTriangle Avatar asked Jan 25 '26 05:01

GreenTriangle


1 Answers

Yes, Cmd.batch can batch multiple cmds into one cmd.

For example (via new message: FetchAll):

FetchAll idsStr ->
  let
    cmds = String.split "," idsStr |> List.map (\id -> getInfo id)
  in (model, Cmd.batch cmds)

also, (model, Cmd.batch cmds) can be written as model ! cmds

like image 63
Tosh Avatar answered Jan 27 '26 00:01

Tosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!