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

末鹿安然 提交于 2019-11-30 04:20:54

问题


I want to load user profile before rendering something into the page but the whole user profile is composed of different parts that are loaded by multiple HTTP requests.

So far I'm loading user profile in sequence (one by one)

type alias CompanyInfo = 
  { name: String
  , address: ...
  , phone: String
  , ...
  }

type alias UserProfile = 
  { userName: String
  , companyInfo: CompanyInfo
  , ... 
  }

Cmd.batch
  [ loadUserName userId LoadUserNameFail LoadUserNameSuccess
  , loadCompanyInfo userId LoadCompanyInfoFail LoadCompanyInfoSuccess
  ...
  ]

But that's not very effective. Is there a simple way how to perform a bunch of Http requests and return just one complete value?

Something like this

init = 
    (initialModel, loadUserProfile userId LoadUserProfileFail LoadUserProfileSuccess)

....

回答1:


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.



来源:https://stackoverflow.com/questions/39392469/how-to-perform-multiple-http-requests-tasks-in-bulk-in-elm-lang

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