Please can anyone explain this function to me (ES6)

const tap = (value) =>
  (fn) => (
    typeof(fn) === 'function' && fn(value),

tap('espresso')((it) => {
  console.log(`Our drink is '${it}'`)

Ok I understand that tap accept an argument called value that later is espresso and returns another function with an argument (fn). Then that function checks if his argument is a function and if so then it calls that function, Then it returns value (even if fn is not a function) (And yes I understand that tap is a combinator, I just no get how it is working)

So when calling tap values becomes espresso and fn is this function right?

((it) => {
      console.log(`Our drink is '${it}'`)

So I understand that fn is that anonymous function that accepts (it) as argument. Then I returns console.log() and I understand that ${it} used to show the string on ‘value’. But I don’t get how did it become value. So how does it get espresso??? And everything I just said is correct?

Thank you

On line 3 of your code, ‘it’ receives ‘value’ here.


Something like this


// substitute fn for foo, value for it
function foo(it) {
    console.log(`Our drink is '${it}'`)

Let me know if you need more explanation.

1 Like

@gunhoo93 ooooh got it!! Thank you. It was so obvious, I don’t know how I missed that :frowning:I was looking for the argument after the function and forget completly that It was getting called inside >.<

@P1xt Yes, I was understanding currying but I was somehow just missing that fn got value as parameter after calling himself (I was looking for the parameter in the function call for tap instead of on the declaration). Also I took me off that it is calling himself using the argument instead of a name . Oh nvm is no calling himself, it is calling the function that is getting as argument!! holy this is somehow that they don’t have names is confusing me but is really amazing. Thank you too.