榆树:链Http.send和Http.get

时间:2016-05-09 13:04:01

标签: elm

我(初学者)有一个类型问题试图链接到我的Elm应用程序中的HTTP调用:

Http.send ... `Task.andThen` (\_ -> Http.get ...)

这是因为Http.send返回类型为Task RawError Response, 并且Http.get返回类型为Task Error value

有关如何让它们协同工作的任何建议吗?

EDIT1:

也许mapError是解决方案?

EDIT2:

我并不是说第一次通话失败了,我确定它有效。编译器没有验证我的代码:

The right argument of `andThen` is causing a type mismatch.

135│     Http.send Http.defaultSettings config
136│>      `Task.andThen` (\_ -> Http.get (Json.Decode.list userJsonDecoder) "http://localhost:3000/")

`andThen` is expecting the right argument to be a:

    Http.Response -> Task Http.RawError a

But the right argument is:

    Http.Response -> Task Http.Error (List User)

1 个答案:

答案 0 :(得分:1)

您需要一种方法将RawError映射到Error,然后您可以按照第一次编辑中的建议使用Task.mapError。一种可能性是:

rawErrorToError : Http.RawError -> Http.Error
rawErrorToError rawError =
  case rawError of
    Http.RawTimeout -> Http.Timeout
    Http.RawNetworkError -> Http.NetworkError

Http.send Http.defaultSettings config
|> Task.mapError rawErrorToError
`Task.andThen` ...