# Elm Design Question: Task vs Cmd msg

**URL:** <https://discourse.elm-lang.org/t/elm-design-question-task-vs-cmd-msg/3890>\
**Category:** Learn\
**Created:** [June 27, 2019, 1:28pm UTC](https://discourse.elm-lang.org/t/elm-design-question-task-vs-cmd-msg/3890 "2019-06-27T13:28:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RyanNHG](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ryannhg/32/5162_2.png) [@RyanNHG](https://discourse.elm-lang.org/u/RyanNHG)\
**Post date:** [June 27, 2019, 1:28pm UTC](https://discourse.elm-lang.org/t/elm-design-question-task-vs-cmd-msg/3890/1 "2019-06-27T13:28:49Z")

</div>

Hey folks,

This might be a silly question, but I was curious why there is a distinction between `Task` and `Cmd msg`

I was converting a task to a command the other day with `Task.perform` and was curious why Elm programs have this:

```elm
update : Msg -> Model -> (Model, Cmd msg)

```

Instead of something like this:

```elm
update : Msg -> Model -> (Model, Task)

```

Thanks!  
Ryan

---

<div class="post-metadata">

**Author:** ![opvasger](https://avatars.discourse-cdn.com/v4/letter/o/8e8cbc/32.png) [@opvasger](https://discourse.elm-lang.org/u/opvasger)\
**Post date:** [June 27, 2019, 6:07pm UTC](https://discourse.elm-lang.org/t/elm-design-question-task-vs-cmd-msg/3890/2 "2019-06-27T18:07:50Z")

</div>

I think about commands as anything you can ask the Elm runtime. Tasks is a subset of there where failure (mostly) is an option.

I don’t mean to change the subject, but I think tasks without error could probably be commands instead, so tasks could be all about dealing with errors!

---

<div class="post-metadata">

**Author:** ![digitalsatori](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/digitalsatori/32/3824_2.png) [@digitalsatori](https://discourse.elm-lang.org/u/digitalsatori)\
**Post date:** [July 2, 2019, 1:38am UTC](https://discourse.elm-lang.org/t/elm-design-question-task-vs-cmd-msg/3890/3 "2019-07-02T01:38:41Z")

</div>

> [@opvasger](#):
>
> I think tasks without error could probably be commands instead, so tasks could be all about dealing with errors!

Task is not all about dealing with errors. Task has to be converted to Cmd to be executed by the Elm Runtime, and the execution of the Cmd may cause errors.  
Our Elm code world is pure, so all the side effects are dealt by the Elm Runtime. `Cmd Msg` is data itself, when it is passed to the Runtime, it is like saying

> “hey Runtime, can **You** do this for me, when it’s done, pass back the `Msg`.”

Whilst task is living in the Elm world, it is data too, it is like saying

> ’ **I** plan to do the follow things’

It is only when it is passed to `Task.perform` or `Task.attempt` as an argument that it is converted to `Cmd` and thus be executed by Elm runtime.

Tasks can be chained together, so it is very useful when you need a list of Cmd to be executed in sequential order.

---

<div class="post-metadata">

**Author:** ![pdamoc](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pdamoc/32/36_2.png) [@pdamoc](https://discourse.elm-lang.org/u/pdamoc)\
**Post date:** [July 2, 2019, 6:27am UTC](https://discourse.elm-lang.org/t/elm-design-question-task-vs-cmd-msg/3890/4 "2019-07-02T06:27:58Z")

</div>

> [@RyanNHG](#):
>
> This might be a silly question, but I was curious why there is a distinction between `Task` and `Cmd msg`

This is not a silly question. This topic can be quite tricky.

Tasks are guaranteed to complete. Cmds are not guaranteed to complete.

If you think in terms of message passing, Tasks are like a request-response synchronous type of communication while Cmds are asynchronous messages.

If you call to JS through a port, expect a reply and forget to send the reply from JS, you might have something like a “leak”. You can mitigate that by introducing a timeout but the decision was made that it would be best to keep the communication with JS asynchronous.

---

<div class="post-metadata">

**Author:** ![dta](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dta/32/1917_2.png) [@dta](https://discourse.elm-lang.org/u/dta)\
**Post date:** [July 2, 2019, 12:05pm UTC](https://discourse.elm-lang.org/t/elm-design-question-task-vs-cmd-msg/3890/5 "2019-07-02T12:05:49Z")

</div>

> [@pdamoc](#):
>
> Tasks are guaranteed to complete. Cmds are not guaranteed to complete.

I know that this is where elm philosophically draws a line, but I find it really rather arbitrary. Tasks can’t really be guaranteed to complete unless the program is guaranteed to execute for an arbitrary amount of time. Also, there’s no guarantees built into the language about the guaranteed completion of tasks, so you still have to write your program as if you could get the resulting message 0 or many times.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [July 12, 2019, 12:05pm UTC](https://discourse.elm-lang.org/t/elm-design-question-task-vs-cmd-msg/3890/6 "2019-07-12T12:05:51Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
