1

Was just wondering if in javascript or typescript there is a way to write the conditional of a fat arrow function in the element mapping itself?

Current code

functionName(data => {
  if(data){
  }
});

Is there syntax that checks data before it even gets to the if statement?

Something like

functionName(data? => {
   if(data){ // This code is then not needed
   }
});

Or

functionName(!data => {
   if(data){ // This code is then not needed
   }
});

So that it only passes into the function if data is falsey.

Thanks

arkade
  • 1,079
  • 1
  • 7
  • 13
  • 3
    can you do so in a "normal" function? – Jaromanda X May 16 '18 at 09:22
  • 1
    please add an example of what you like to do and what you like to get. – Nina Scholz May 16 '18 at 09:22
  • @NinaScholz - I think it wants some notation where a function is conditionally executed without an `if` or similar statement – Jaromanda X May 16 '18 at 09:24
  • why "not null" ... because `data` will be falsey if it is `undefined` or `0` or `''` or `false` ... your requirement of `not null` is quite specific - anyway, what's wrong with `if (data === null) return;` as the very first statement in the function? – Jaromanda X May 16 '18 at 09:25
  • [There is no way, use a normal function](https://stackoverflow.com/questions/9906009/call-a-function-only-if-a-value-is-neither-null-nor-undefined). – user202729 May 16 '18 at 09:26
  • 1
    So `functionName` is a function that takes another function as an argument. In that case, the function `functionName` could just implement logic to not call the argument function if `data` is null or undefined. It would be a lot easier to answer this question if you'd include a decent example though. – Robby Cornelissen May 16 '18 at 09:27
  • 1
    No there is nothing wrong with the if statement , am just curious if there is a short hand , but yeah basically like you say `falsey` not `not null` specific – arkade May 16 '18 at 09:27
  • @RobbyCornelissen Yeah that makes sense, the component i am working with is an alert box , i am hitting the onDidDismiss of the alert which is triggered is the user clicks outside of the alert but also if the user clicks an option in the alert, so i just need to handle for both cases – arkade May 16 '18 at 09:34
  • Please share your implementation of `functionName`. That's probably the best place for any `null` or **falsey** checks. – JJWesterkamp May 16 '18 at 09:36
  • @JeffreyWesterkamp well it is `handler: data => { alert.dismiss(data); }` but it is also triggered in the component itself, I guess im taking a shortcut because the correct implementation is to use buttons in the alert. Its not a huge issue, i just keep stumbling on typescript shortcuts, so was just wondering if something like this existed – arkade May 16 '18 at 09:41
  • Okay, the shortest way would be to _short circuit_ the `alert.dismiss` expression: `handler: (data) => { data && alert.dismiss(data) }`. This will not call `alert.dismiss` if `data` is falsey. Any call to `handler` would still execute though. – JJWesterkamp May 16 '18 at 09:45

2 Answers2

1

Why don't you check the data before you run the function?

functionName(data => {
  if(data){
  }
});

Instead of that, we use:

functionName(data => {
  // do something with data
});

data && functionName(data); // call the function when data is valued 

Basically, there have no ways to check in the function as your expectation because the function is fired and use data you provided as a param immediately. data is just used as param in your function, no more roles of outside. So While you can access the data, it means you are inside of the function already.

Sakata Gintoki
  • 1,817
  • 16
  • 23
  • Its because the function can be triggered outside my code ( in the component ) I mean i can do a check before but it would only work with the actions on specific user actions ... I would miss one action from the user if they click outside the alert. Its not a issue really was just wondering if typescript or es had a shortcut for this, there seems to be a lot of helpful shortcuts ... so was wondering – arkade May 16 '18 at 14:02
0

Basically what you are doing with is calling functionName() with an anonymous arrow function as a parameter. Also the arrow function takes one parameter named data.

So when you check data?, it means you are already called functionName with an anonymous arrow function as a parameter. So basically, I don't think you can do what you asked for.

snnsnn
  • 10,486
  • 4
  • 39
  • 44