task - 如何在 thenable 链上等待第一个到达的结果?
问题描述
我们可以使用Promise.race等待thenable
链上第一个到达的结果。Task模块似乎还不支持它,Task.sequence仅相当于Promise.all。
Non-thenable解决方案演示:
import Process
import Task
init () =
( Nothing, Cmd.batch [ after 2 "2nd", after 1 "1st" ] )
after seconds name =
Process.sleep (1000 * seconds)
|> Task.map (always name)
|> Task.perform Done
type Msg
= Done String
update (Done name) model =
case model of
Nothing ->
( Debug.log name <| Just name, Cmd.none )
_ ->
( Debug.log name model, Cmd.none )
main =
Platform.worker
{ init = init
, update = update
, subscriptions = always Sub.none
}
运行它,按预期输出:
1st: Just "1st"
2nd: Just "1st"
解决方案
Promise.race
作为一个独立的函数,需要维护本地状态来跟踪它是否已经被解析,你可能知道这在 Elm 中是不可能的。
但是您可以通过自己跟踪模型中的状态来相对轻松地完成相同的事情。这是一个Maybe
用于跟踪我们是否收到响应的示例:
type Thing =
...
getThings : String -> Task Never (List Thing)
getThings url =
...
type alias Model =
{ things : Maybe (List Thing) }
type Msg
= GotThings (List Thing)
init =
( { things = Nothing }
, Cmd.batch
[ Task.perform GotThings (getThings "https://a-server.com/things")
, Task.perform GotThings (getThings "https://a-different-server.com/things")
]
)
update msg model =
case msg of
GotThings things ->
case model.things of
Nothing ->
( { things = Just things }, Cmd.none )
Just _ ->
-- if we have already received the things, ignore any subsequent requests
( model, Cmd.none )
view model =
...
推荐阅读
- node.js - Dialogflow:检查用户是否连续发送 X 条消息
- python - 如何在python中删除或清除打印的文本
- javascript - 如何使用带有 CSS-in-JS 的 React 重写此按钮及其悬停效果
- ios - 使用“弹出式”VC 时,背景模糊效果无明显原因
- php - 在没有数据库的情况下保存和编辑文本数据的最佳方法
- python - 我如何为字符串的最终出现制作正则表达式
- java - 如何在循环中使最接近的数字为零
- c# - 如何在 c# 中将图像保存为具有透明度的 gif?
- android - 如果 VPN 应用程序被杀死,它会发生什么?
- java - 应用程序关闭时如何每小时触发一次警报