How to get the value from (task string value)?

I am trying to load json from localstorage with this code:

let 
    val = Storage.getItem "exercises" decodeExerciseList
in 
    ({ model | exercises = val }, Cmd.none)

but I get this error: the 5th and 6th branches of this caseproduce different types of values. - The 5th branch has this type:

( Model, Cmd Msg )

But the sixth:

( { exercise : Maybe Model.Exercise
, exercises : Task String (List Model.Exercise)
}
, Cmd msg
)

I thought maybe this could help:

case val of
    succeed -> ({ model | exercises = val }, Cmd.none)
    fail -> ({model | exercises = []}, Cmd.none)

but no luck. in this case, I got another error:

The 1st and 2nd branches of this caseproduce different types of values. - 1st branch has this type:

( { ..., exercises : Task String (List Model.Exercise) }, Cmd msg )

But the second one:

( { ..., exercises : List a }, Cmd msg )

so basically I'm still having a problem with Task String X instead of X.

any idea what to do here?

+4
source share
1 answer

localstorage javascript, , , Elm, , .

, 2  -  -

-

type Msg 
    = LoadFromStorage 
    | OnLocalStorage (Result String (List Model.Exercise))

update message model =
    case message of 
        LoadLocalStorage -> 
            ( model
            , Storage.getItem "exercises" decodeExerciseList
                |> Task.attempt OnLocalStorage
            )
        OnLocalStorage res -> 
            case res of 
                Ok val -> 
                    ({ model | exercises = val }, Cmd.none)
                Err err -> 
                    handle error
+7

Source: https://habr.com/ru/post/1693470/


All Articles