Cover image for Call function only when a value passed validation in javascript

Call function only when a value passed validation in javascript

rajnishkatharotiya profile image Rajnish Katharotiya ・2 min read

In the normal app, we always came across to function call only if one function returns positive or desired value. Yes? if yes, comment me on how you'd achieve it? πŸ€—

Before going further, I would like to welcome you to a new episode of series call Javascript Useful Snippets. In this series, I'm sharing some shortcodes and useful functions that can let you make your code faster and neat. So, if you haven't read my previous episodes articles please check it out here or else stay tuned till the end to learn something new πŸ˜‹ .

I hope you have shared your answers in a comment. Well, mine is, I've defined one function called when(). This method will take two arguments one will be prediction function and another will be a function ( which needs to call after ).

How does this when() function works?

const when = (pred, whenTrue) => x => (pred(x) ? whenTrue(x) : x);

Here, as you see we have passed two arguments, prediction ( as pred ) and function ( as whenTrue ) in first arguments list. Before explaining further let me clerify one concept called carried function.

What is carried function?

It's a way to call multi-functions in one call. Let me provide one example, guess you want to add two numbers...

adding numbers:

const add = (a, b) => a + b;
add(3, 2) // 5

adding numbers in a carried way :

const add = a => (b => a+b);
const add3  = add(3)
add3(2) //5

which means we are simply calling a function inside a function or returning function inside function let's see a normal way of doing it is:

const add = function (a) {
  return function (b) {
    return a + b

Now, you have an idea about carried function syntex ( feel free to comment if it's unclear ) you can see I've defined when as a carried function which means it'll pass when function call parameters as first arguments list and call of this function will be passed as second argument list ( explain more clearly in function usage section ) and return of function, I've called pred function with an argument x and checked if it's true then returning whenTrue(x) ( callback/after function ) otherwise x itself is returned. Let's use it with one example.

How to use When() function ?

const divideNumber = when(x => x % 2 === 0, x => x / 2);
divideNumber(4); // 2
divideNumber(3); // 3

So, here i've first difined one function by using when() to divide integer by two if it's dividable with two ( means it should return integer only after dividation ) and when i passed first value, it's returing value after divition it but in case of secound value it's returing same as output.

I found When() function useful to validate value before passing it to other functions. So, I thought to share it with you too. I hope you liked my explanation (if yes, hit like ❀️ button ) and if you found it informative then do follow from here because I'll learn and share every day.πŸ˜‹

Also follow/subscribe me on my social media account to connect with me : twitter, youtube

Posted on Apr 12 by:

rajnishkatharotiya profile

Rajnish Katharotiya


JavaScript enthusiast, Front-end developer, Blogger & YouTuber (https://www.youtube.com/channel/UCvNjso_gPQIPacA6EraoZmg)


markdown guide