Alternative to multiple nested conditional if-then?
Options
Valentin R
Member ✭
I often find myself in a situation when I need to use multiple nested if-then conditions based on some value in the incoming data. Is there any other way to "branch" out the flow of the functions?
E.g. I have some "segment id" for all the entries coming into the system and I need the endpoint to apply different functions based on that segment id. To do it right now I need to nest a lot of conditional if-then nested under each other, for every segment id value. Anything else I can do to perform such "branching"?
Thanks
E.g. I have some "segment id" for all the entries coming into the system and I need the endpoint to apply different functions based on that segment id. To do it right now I need to nest a lot of conditional if-then nested under each other, for every segment id value. Anything else I can do to perform such "branching"?
Thanks
Comments
-
I'm going to chime in though I don't have a clear answer.. .. .. more a feature request to the Xano team. In some programming languages including JavaScript there is a "switch" statement. Maybe there is a way to access that functionality in Xano now?
The other thing that may make your code easier to understand later is to create your APIs as stand alone code blocks. Then convert the APIs into a function (3 dots - upper right) and find the function later in the library segment of the page on the left. In your block of multiple conditionals.. .. .. one after the other, insert the function you just created. Much easier to read your code later. -
good point, thanks David. I'm also breaking things down as much as possible but sometimes it's not convenient — you lose visibility and editability of things in sub-functions.
I'll look into the "switch" statement. Maybe it's now accessible through lambdas, which are not yet known to me. -
Hi Valentin,
If I'm understanding your problem correctly perhaps you need to try the precondition function under "Utility Functions". Preconditions automatically checks and enforces for a condition to be true. This is an easy fix for reducing if-then statements.
Let me know if this helps! -
Some related magic is on the way...
https://community.xano.com/c/request-a-feature/try-catch-switch-case -
saw this thread, yep. Looking forward
-
I'm using those where I can, but any failed precondition stops the process. Thus, you can't use multiple to mimic the "switch" statement mentioned by David above. Or am I missing something?
[image.png] -
Hey guys the try/catch is on the roadmap. If enough of us upvote it, perhaps Xano's leadership can have more focus on it. I gave my vote.
[image.png]
Hi and can you guys make some videos using the lambda features with Xano? It will give me some ideas of how to do it and learn more to do other stuff that I want to do.
Categories
- All Categories
- 53 ? Announcements
- 47 ? Releases
- 37 ? Welcome
- 983 ? Help! I'm a Noob
- 125 ? No-Code Front-Ends
- 633 ? Working with APIs
- 439 ? Transforming data
- 126 ? Connect Xano to ...
- 50 ?? Find an Expert
- 348 ❓Other questions
- 35 ? Security
- 22 ✂️ Snippets
- 19 ? Showcase
- 7 ?️ Xano Chatter
- 62 ? Video Tutorials
- 171 ? Request a feature
- 229 ? Report a Bug
- 19 ? Templates & Extensions
- 7 ? Feedback