Emi*_*Emi 3 javascript ramda.js
const fetchAmount = () => new Promise((resolve) => setTimeout(() => resolve({value: 5}, 1000)))
const getTotal = pipeP(
fetchAmount,
prop('value'),
add(2)
)
await getTotal() //=> 7
Run Code Online (Sandbox Code Playgroud)
而且我已经看到它已被弃用,我发现的唯一解决方案是添加then,例如:
const fetchAmount = () => new Promise((resolve) => setTimeout(() => resolve({value: 5}, 1000)))
const getTotal = pipeP(
fetchAmount,
then(prop('value')),
then(add(2))
)
await getTotal() //=> 7
Run Code Online (Sandbox Code Playgroud)
这是要走的路吗?我想弃用可能有重要的原因,pipeP因为将 promise 与纯函数结合使用时它真的很容易使用。
是的,这在v0.26.0中已被弃用。
Ramda 添加了pipeWith和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) //~> 22Run Code Online (Sandbox Code Playgroud)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
<script>const {unapply, pipeWith, then} = R </script>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
656 次 |
| 最近记录: |