有没有办法将这个异步函数重构为无指向?
Is there a way to refactor this async function to point free?
我有这个功能:
const getData = async player => {
const [profile, repos] = await Promise.all([
getProfile(player),
getRepos(player)
])
return {
profile,
repos
}
}
我可能不需要更改它,但我只是好奇是否可以使用 Ramda 使其免费。
我认为你已经清楚地传达了意图,重构到无点可能会使事情变得混乱。
就是说,这可能看起来像一个例子:
const getProfile = player => Promise.resolve('player-' + player)
const getRepos = player => Promise.resolve('repo-' + player)
const fn = R.pipeP(
R.pipe(R.juxt([getProfile, getRepos]), Promise.all.bind(Promise)),
R.zipObj(['profile', 'repos'])
)
fn('foo').then(console.log)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
这利用了 R.juxt
to create a function that passes it's argument to each function in the provided array of Promise-producing functions and R.pipeP
to compose R.zipObj
和生成的 Promise。
我有这个功能:
const getData = async player => {
const [profile, repos] = await Promise.all([
getProfile(player),
getRepos(player)
])
return {
profile,
repos
}
}
我可能不需要更改它,但我只是好奇是否可以使用 Ramda 使其免费。
我认为你已经清楚地传达了意图,重构到无点可能会使事情变得混乱。
就是说,这可能看起来像一个例子:
const getProfile = player => Promise.resolve('player-' + player)
const getRepos = player => Promise.resolve('repo-' + player)
const fn = R.pipeP(
R.pipe(R.juxt([getProfile, getRepos]), Promise.all.bind(Promise)),
R.zipObj(['profile', 'repos'])
)
fn('foo').then(console.log)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
这利用了 R.juxt
to create a function that passes it's argument to each function in the provided array of Promise-producing functions and R.pipeP
to compose R.zipObj
和生成的 Promise。