# Using Task in init

**URL:** <https://discourse.elm-lang.org/t/using-task-in-init/6119>\
**Category:** Request Feedback\
**Created:** [August 3, 2020, 4:24am UTC](https://discourse.elm-lang.org/t/using-task-in-init/6119 "2020-08-03T04:24:57Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [August 3, 2020, 7:46am UTC](https://discourse.elm-lang.org/t/using-task-in-init/6119/2 "2020-08-03T07:46:54Z")

</div>

If you need this, you can do it with the current structure by marking the model as uninitialized.

```auto
type alias Size = 
    { width : Float
    , height : Float
    }

type alias Model =
    { size : Maybe Size 
    }

init : () -> ( Model, Cmd Msg )
init _ =
    (Model Nothing, Browser.Dom.getViewport |> Task.perform Initialize )

type Msg 
    = Initialize Browser.Dom.Viewport
    ...

update : Msg -> Model -> (Model, Cmd Msg) 
update msg model = 
    case msg of 
        Initialize viewport -> 
            (Model {width = viewport.width, height = viewport.height}, Cmd.none)
        ...

view : Model -> Html Msg 
view model = 
     Maybe.map viewForSize model.size 
     |> Maybe.withDefault (text "")

```

If you don’t like the `Maybe` you can use a flag `isInitialized` or you can make the Model a custom type like ` type Model = Initializing | Initialized State`.

---

_[View the full topic](https://discourse.elm-lang.org/t/using-task-in-init/6119)._
