Putzi San
Putzi San

Reputation: 6301

Ramda ifElse is not executed for function with multiple arguments

Can somebody explain me this behavior, or is it a bug?

const firstTest = (a) => console.log(a, 'this will be executed');
const secTest = (a, b) => console.log(a, 'this will not be executed');

const firstIfElse = R.ifElse(R.T, firstTest, () => null);
const unexpectedIfElse = R.ifElse(R.T, secTest, () => null);

firstIfElse('logging appears as expected');
unexpectedIfElse('no logging');

example in REPL

Upvotes: 1

Views: 778

Answers (1)

Scott Sauyet
Scott Sauyet

Reputation: 50787

Your second function is a curried binary function. ifElse chooses the maximum arity of the three functions passed to it, predicate, ifTrue, and ifFalse. R.T has arity 1, as does () => null, but secTest has arity 2, so unexpectedIfElse also has arity 2.

When you call unexpectedIfElse with 'no logging', you get back a function waiting for the (useless) b parameter.

There are reasons not to like this additional complexity, but there are times when it's very useful, especially for the predicate.

You can fix your issue by calling it like

unexpectedIfElse('no logging', 'ignored');

or like

unexpectedIfElse('no logging')('ignored')

Upvotes: 2

Related Questions