Ramda 中已弃用的 pipeP 的替代方案 [英] Alternative to deprecated pipeP in Ramda
问题描述
我目前使用 Ramda 的 pipeP 实现了类似的实现:
I currently have something like this implementation using Ramda's pipeP:
const fetchAmount = () => new Promise((resolve) => setTimeout(() => resolve({value: 5}, 1000)))
const getTotal = pipeP(
fetchAmount,
prop('value'),
add(2)
)
await getTotal() //=> 7
而且我已经看到它已被弃用,我找到的唯一解决方案是添加 then
,例如:
And I've seen that it's deprecated and the only solution I found is adding then
, like:
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 与纯函数结合使用时它真的很容易使用.
Is this the way to go? I guess there might be important reasons to deprecate pipeP
because it was really easy to use when combining promises with pure functions.
推荐答案
是的,这在 中已被弃用v0.26.0.
Ramda 添加了 pipeWith
和 composeWith
,涵盖了更广泛的范围.
Ramda added pipeWith
and composeWith
, which covered a wider spectrum.
pipeP (f1, f2, ..., fn)
可以写成 pipeWith (then) ([f1, f2, ..., fn])
.
如果你想要完全相同的签名,你可以这样写:
If you want the exact same signature, you can write something like this:
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>
这篇关于Ramda 中已弃用的 pipeP 的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!