How to Stop Repeat / break out of the loop?

Hi there,

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.

Thanks

  1. 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;)

  2. 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

Disclaimer: I haven’t tried any of those.

1 Like

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.

2 Likes

Thank you both I’ll try them both out tonight. I guess with the while loop I’ll need to iterate through the array myself with a count variable?

Hi Jon,

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:
image

Thanks

Sorry @datguru I meant to specify to do that with a while as you would do with normal code.

Thanks @JonL and @bpj yes the While loop does work if I manually iterate over the array and then check for the stop boolean:
image

2 Likes

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.

Thanks

1 Like

is there any solution for repeat?

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 :slight_smile:

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

Feature request has been opened, vote for it:
https://community.wappler.io/t/break-step-to-break-out-of-while-or-repeat-loops/41909/2