简体   繁体   English

Ramda 中已弃用的 pipeP 的替代方案

[英]Alternative to deprecated pipeP in Ramda

I currently have something like this implementation using Ramda's pipeP :我目前使用RamdapipeP实现了类似的实现:

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

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

await getTotal() //=> 7

And I've seen that it's deprecated and the only solution I found is adding then , like:而且我已经看到它已被弃用,我发现的唯一解决方案是添加then ,例如:

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

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

await getTotal() //=> 7

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.我想弃用pipeP可能有一些重要的原因,因为将 promise 与纯函数结合使用时它真的很容易使用。

Yes, this was deprecated in v0.26.0 .是的,这在v0.26.0中已被弃用。

Ramda added pipeWith and composeWith , which covered a wider spectrum. pipeWith添加了pipeWithcomposeWith ,涵盖了更广泛的范围。

pipeP (f1, f2, ..., fn) can be written as pipeWith (then) ([f1, f2, ..., fn]) . 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM