Just trying to figure out how to break out of a repeat in server connect when I have found what I am looking for. I’m essentially looking for a break; function like in PHP.
Does anyone know how to accomplish this in Wappler please?
I have tried adding a response action in server-connect but it continues on through the repeat.
I guess you could build a custom module to do that but you probably need some info from Wappler on how the whole repeat works and it will also depend on server model. (nodejs and PHP -> break;)
Also if you can wait until the next evaluation of the condition for the repeat you could break there with a flag.
Your repeat condition: {{array/object && !stop}}
When you want to break the repeat set stop = true
You could use while rather than repeat for some things…
I tend to set a value to true before the loop starts. Set the while loop going with the expression looking at the value and when the item I want to find/action is complete I set the value to false to finish the while loop.
Just wondering how do I set !stop through the GUI. I can write it directly in code view but just wondering if there is a way to do it through the GUI end?
Unfortunatley I tried the Repeat method and I don’t think it works, it doesn’t seem to iterate through the items if I add a conditional parameter there. I wodner if @Teodor could confirm if repeats do work with conditions and if I am just doing something wrong? This is what I ended up trying in the repeat:
Or you could use a repeat and put the entire content in a condition based on a variable you set within the loop.
That’s probably the same as the ‘while’ version though, but you don’t need to index the data… and the repeat will keep running even when the condition is met.
Thanks Anthony, thats what I am trying to avoid. Running the rest of the query when I dont need to process anymore. I think the while works well for this for the time being but would be great if its possible to make repeats work the same as they are much nicer for iterating through the array without me needing to have a counter.
Intentionally crash the script when you want to break out of the repeat. Encapsulate the repeat within a Try/Catch so you can recover from the crash. In this example, it’ll crash on the first iteration after setting the variable “out”, so only one “out” will be defined
Obviously, the Wappler team needs to build a proper “Break” step instead of relying on this workaround I just came up with. Hope it helps meanwhile
The rest of your script goes outside the Try/Catch - only use the Try/Catch for the repeat. You don’t need a Catch, the Catch is just there for debugging purposes