INTRODUCTION
Hi community, my name is Doye but everyone calls me D. I am a Flutter developer and a big no-code enthusiast too. I adpoted Weweb as my web platform of choice 2 months ago and it has been amazing . I often like to contribute to the no-code communities I belong to (and I am looking forward to the marketplace). But today I wanted to share a decent no-code solution to a question I have seen come up quite a lot in the community forum
THE PROBLEM - How do I perform a repeated action until something happens, aka, the While Do workflow
This is often needed when you need to poll an endpoint for example and you wanna keep doing that till you get a response. There is no direct way to do it in Weweb, but when I needed to do it, I found a way to get it done safely without triggering infinite recursion.
A THE PARAMETERS -
- Create a global workflow call it āloop untilā (or anything you want), give it two parameters ā max_loop a type number and delay a type number too.
Max loop will be used by you to set an upper limit for your loop function (you know like a max retry), this is because you donāt want your action going on forever if thereās a bug for instance).
delay is the gap in miliseconds you wanna have between the loops. This is important because you donāt wanna flood the server youāre polling.
B. THE HELPER FUNCTION
Next you get a function that generates a random array any length that you tell it to (donāt worry, just ask the AI to generate it for you).
C. THE WORKFLOW ITSELF
The workflow is simple. On execution you open a for loop (or iterator as we call it here)
-
The array the for loop will iterate over is a random array of length = the max number of loops you set (that array you would generate from your helper function remember?)
-
Now the loop is open, the first action in that loop is whatever the logic you wanna repeat.
-
Next put a true/false split to evaluate your stop condition (the thing that you will get to make you stop polling e.g. a 200 response status)
-
After the true/false block, you put a delay timer whose value = the delay parameter you picked when calling the function.
-
Now for what is in the false arm of the true/false you can leave it blank and do nothing
-
For what is in the true arm. This is what you execute when your stop condition is met and you wanna exit the loop. For this, just put a custom javascript code there and in the code write break; Thatās all, nothing else, just break.
Thatās it. This workflow helps me process an item that I needed to poll every 3 seconds for 3 minutes no problems.
Note that the break will actually force an error which will kill the action. (So if you need to do anything before exiting the loop onSuccess, uno, maybe send a success message or something, make sure you put it BEFORE the break)
Workflow images below
complete workflow structure
Iām happy to answer any questions you may have. This way you can long poll on weweb safely with minimal risk of flooding your server or infinite loop.
PS: because it is running as an async execution the loop WILL CONTINUE without blocking other actions even if you leave the page where you called it. So you can use it for āChron-likeā tasks (PS, you should do chrons in backend).