替代 Ramda 中已弃用的 pipeP

Alternative to deprecated pipeP in Ramda

我目前有类似这个使用 Ramda 的实现的东西 pipeP:

const fetchAmount = () => new Promise((resolve) => setTimeout(() => resolve({value: 5}, 1000)))

const getTotal = pipeP(
  fetchAmount,
  prop('value'),
  add(2)
)

await getTotal() //=> 7

而且我发现它已被弃用,我找到的唯一解决方案是添加 then,例如:

const fetchAmount = () => new Promise((resolve) => setTimeout(() => resolve({value: 5}, 1000)))

const getTotal = pipeP(
  fetchAmount,
  then(prop('value')),
  then(add(2))
)

await getTotal() //=> 7

这是要走的路吗?我想可能有一些重要的理由要弃用 pipeP,因为在将 promise 与纯函数结合使用时它真的很容易使用。

是的,这在 v0.26.0 中已弃用。

Ramda 添加了 pipeWith and composeWith,涵盖了更广泛的范围。

pipeP (f1, f2, ..., fn)可以写成pipeWith (then) ([f1, f2, ..., fn]).

如果你想要完全相同的签名,你可以这样写:

const pipePromises = unapply (pipeWith (then))

pipePromises (
  (n) => Promise .resolve (n + 1),
  (n) => Promise .resolve (n * n),
  (n) => Promise .resolve (n - 3)
) 
(4) 
.then (console .log)  //~> 22
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
<script>const {unapply, pipeWith, then} = R              </script>