Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using the fetch api with ramda

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.com/todos/1')
  .then(toJSON)
  .then(console.log)
  
// Does not Work
fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(toJSONRamda)
  .then(console.log)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
like image 313
3Dos Avatar asked Oct 09 '18 13:10

3Dos


People also ask

Is ramda better than Lodash?

Ramda is generally a better approach for functional programming as it was designed for this and has a community established in this sense. Lodash is generally better otherwise when needing specific functions (esp. debounce ).

Should I use ramda?

Yep. Ramda is an excellent library for getting started on thinking functionally with JavaScript. Ramda provides a great toolbox with a lot of useful functionality and decent documentation. If you'd like to try the functionality as we go, check out Ramda's REPL.

What is Ramda used for?

Ramda is a JavaScript library that makes functional programming simple. While JavaScript libraries with functional capabilities have been around for a while, Ramda acts as a complete standard library specifically catered to the functional paradigm. Ramda is a JavaScript library that makes functional programming simple.


1 Answers

The 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.com/todos/1')
  .then(toJSONRamda)
  .then(console.log)
<script src="//cdnjs.cloudflare.com/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.com/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.

like image 150
Scott Sauyet Avatar answered Oct 30 '22 10:10

Scott Sauyet