【发布时间】:2017-11-07 18:40:19
【问题描述】:
我是 Reason 的新手,目前正在尝试将个人项目从 js 转换为 Reason。到目前为止,除了异步的东西之外,它一直很容易。
我无法延迟递归地调用我的函数。
我有一个函数getPrice,它返回一个 int 的承诺
type getPrice = unit => Js.Promise.t(int)
我想创建另一个函数checkPrice,它会根据给定的用户价格无休止地检查当前价格,除非满足条件。
let rec checkPrice = (userPrice) =>
Js.Promise.(
getPrice()
|> then_(
(currentPrice) =>
if (currentPrice >= userPrice) {
resolve(currentPrice)
} else {
/* call this function with setTimeout */
checkPrice(userPrice)
}
)
);
但是我得到类型不匹配说setTimeout应该是单元类型
【问题讨论】:
-
避免使用
Promiseconstructor antipattern!制作一个单独的帮助函数,返回一个超时承诺,然后使用承诺链接。
标签: asynchronous promise reason bucklescript