> For the complete documentation index, see [llms.txt](https://orasund.gitbook.io/elm-cookbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://orasund.gitbook.io/elm-cookbook/frequently-asked-questions-1/how-to-turn-a-msg-into-a-cmd-msg.md).

# How to turn a Msg into a Cmd Msg?

{% hint style="danger" %}
**Only Use Sparsely:** Better split the update function into multiple smaller functions.
{% endhint %}

{% tabs %}
{% tab title="Problem" %}

```
type Msg =
  LoginSucceeded User
  | InfoMessage String
  
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    LoginSucceeded newUser ->
      ( { model | currentUser = newUser }
      , Cmd.none
      )
    InfoMessage message ->
      ( { model | message = Just message }
      , Cmd.none
      )
```

{% endtab %}

{% tab title="Solution" %}

```
type Msg =
  LoginSucceeded User
  | InfoMessage String
  
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    LoginSucceeded newUser ->
      ( { model | currentUser = newUser }
      , sendMsg <| InfoMessage "Login Successful"
      )
    InfoMessage message ->
      ( { model | message = Just message }
      , Cmd.none
      )

sendMsg : msg -> Cmd msg
sendMsg msg =
  Task.succeed msg
  |> Task.perform identity
```

{% endtab %}

{% tab title="Alternative Solution" %}

```
type Msg =
  LoginSucceeded User
  | InfoMessage String
  
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    LoginSucceeded newUser ->
      ( model
        |> updateUser newUser
        |> updateMessage "Login Successful"
      , Cmd.none
      )
    InfoMessage message ->
      ( model |> updateMessage message
      , Cmd.none
      )

updateUser : User -> Model -> Model
updateUser user model =
  { model | currentUser = user }

updateMessage : String -> Model -> Model
updateMessage message model =
  { model | message = Just message }
```

{% hint style="success" %}
**This is the a better solution**
{% endhint %}
{% endtab %}
{% endtabs %}

## Question

How can I display a Message after a user logged in?

## Answer

Use the following function:

```
sendMsg : msg -> Cmd msg
sendMsg msg =
  Task.succeed msg
  |> Task.perform identity
```

## Further reading

* 📄**Article:** [How to turn a Msg into a Cmd Msg in Elm?](https://medium.com/elm-shorts/how-to-turn-a-msg-into-a-cmd-msg-in-elm-5dd095175d84) by Wouter In t Velt


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://orasund.gitbook.io/elm-cookbook/frequently-asked-questions-1/how-to-turn-a-msg-into-a-cmd-msg.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
