admin管理员组文章数量:1429729
I'm learning Ramda and try to reach pointfree programming. In order to do that, I try to refactor here and there but got stuck on this.
I obviously think this doesn't work because the call is asychronous but I could not find what is wrong with this code.
// Why is this
const toJSONRamda = R.pipe(
R.prop('json'), // getting the 'json' function
R.call // and calling it
)
// different from this
const toJSON = response => response.json()
// Works
fetch('')
.then(toJSON)
.then(console.log)
// Does not Work
fetch('')
.then(toJSONRamda)
.then(console.log)
<script src=".25.0/ramda.min.js"></script>
I'm learning Ramda and try to reach pointfree programming. In order to do that, I try to refactor here and there but got stuck on this.
I obviously think this doesn't work because the call is asychronous but I could not find what is wrong with this code.
// Why is this
const toJSONRamda = R.pipe(
R.prop('json'), // getting the 'json' function
R.call // and calling it
)
// different from this
const toJSON = response => response.json()
// Works
fetch('https://jsonplaceholder.typicode./todos/1')
.then(toJSON)
.then(console.log)
// Does not Work
fetch('https://jsonplaceholder.typicode./todos/1')
.then(toJSONRamda)
.then(console.log)
<script src="https://cdnjs.cloudflare./ajax/libs/ramda/0.25.0/ramda.min.js"></script>
Share
Improve this question
edited Oct 10, 2018 at 1:46
sideshowbarker♦
88.6k30 gold badges215 silver badges212 bronze badges
asked Oct 9, 2018 at 13:39
3Dos3Dos
3,4973 gold badges26 silver badges39 bronze badges
1 Answer
Reset to default 11The reason this doesn't work is that the json
method of the response object is not a pure function. It is really a method. When you use pipe(prop('json'), call)
, you are trying to call that method as a pure function. In some circumstances that will work. But here, the json
method actually uses this
. Ramda's call
does not supply any this
object.
There is a Ramda alternative:
const toJSONRamda = R.invoker(0, 'json')
fetch('https://jsonplaceholder.typicode./todos/1')
.then(toJSONRamda)
.then(console.log)
<script src="//cdnjs.cloudflare./ajax/libs/ramda/0.25.0/ramda.js"></script>
invoker
works with methods. These should help describe how it works:
R.invoker(0, 'method')(obj) = obj['method']()
R.invoker(1, 'method')(a, obj) = obj['method'](a)
R.invoker(2, 'method')(a, b, obj) = obj['method'](a, b)
//...
However, there is an important point not to miss. Point-free programming is useful only so long as it improves readability. This to me is already perfectly readable:
fetch('https://jsonplaceholder.typicode./todos/1')
.then(resp => resp.json())
.then(console.log)
If this is only a learning exercise, then, by all means feel free to try to turn that into a point-free version. But I would leave it as is for production code.
本文标签: javascriptusing the fetch api with ramdaStack Overflow
版权声明:本文标题:javascript - using the fetch api with ramda - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745532051a2662105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论