Loop over items and waiting logic using modulus (%)

I’m trying to implement a waiting logic when using “Loop over items” node.

On each item processed by the node, I call an API.

Obviously, this API has rate limits I have to avoid.

So, I’m trying to implement a logic like this:

if (currentIndex % 5 === 0) {
   wait(5 seconds)
}

This is a dummy workflow to reproduce the logic (DO NOT EXECUTE IT: infinite loop!)

In the node “If” I’m trying to read the current index processed, but, after many tries, I’m still not able to get this information.

How can I implement a waiting logic when looping over items?

I usually do it like this: I separate the API call or data fetching into its own workflow. In this example, it’s the same workflow. In this simple workflow, I would also implement things like the number of retry attempts and fallback logic. The larger workflow can then process one item after the other. The advantage is that it’s easy to swap out the workflow if, for example, the API is changed to another one.
Personally, I always use the success output data / error output data to clearly define the end of the workflow. But that’s just a matter of personal preference.

@Franz , first of all, thank you for your reply.

Looking at your implementation, I notice yuìou anyway use an HTTP request.

In my real workflow, however, I’m using an Open AI > Message Assistant node, that doesn’t have a distinction between successful and erroring calls.

Can you think at any possible solution to implement this logic without using a direct HTTP Request?

{{ ($('Loop Over Items').context.currentRunIndex + 1) % 5 }}

This can be done using context.currentRunIndex. However, please also remove the reset from the “Loop Over Items” node; otherwise, it will always start over from the beginning, and the run index will not continue counting forward.

1 Like

@Franz , thank you for your guidance.

I’ve updated my test workflow and implemented some debugging console.log().

It seems the loop is never completed: it only processed 11 items and never reacher the done step in the Loop Over Items node: any idea about the reason?

:thinking: it completes on my instance

1 Like

Mmm… do you have a self-hosted instance or a cloud one? Maybe there is some sort of configuration for test workflows? (I’m using a self-hosted instance)

I am also self hosting the community edition

1 Like

Mah, I don’t know why in the test workflow stopped. Anyway, I implementend the same logic in the real workflow and it works like a charm :muscle:

Many thank you for your great and fast support! :pray: