How to perform multiple Http requests (Tasks) in bulk in Elm lang

女生的网名这么多〃 提交于 2019-11-30 20:14:02

You can achieve this using Task.map2:

Edit: Updated to Elm 0.18

Task.attempt LoadUserProfile <|
    Task.map2 (\userName companyInfo -> { userName = userName, companyInfo = companyInfo })
        (Http.get userNameGetUrl userDecoder |> Http.toTask)
        (Http.get companyInfoGetUrl companyInfoDecoder |> Http.toTask)

You can then get rid of the individual LoadUserName... and LoadCompanyInfo... Msgs. In Elm 0.18, the need for separate Fail and Succeed Msgs is addressed by Task.attempt expecting a Result Error Msg type, so that LoadUserProfile is defined like this:

type Msg
    = ...
    | LoadUserProfile (Result Http.Error UserProfile)

map2 will only succeed once both tasks succeed. It will fail if any of the tasks fail.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!