Synchronous Workflow Loop

Hey,

I have a problem with a synchronous workflow loop. I have a main workflow and a secondary workflow. The main workflow contains a loop which should call the secondary workflow a few times. I need a scenario where the loop runs the secondary workflow synchronously, meaning step-by-step, waiting until the workflow ends and then calls it again. But I faced the problem that the loop does not wait until the secondary workflow is finished. I thought it was a Promise problem and with a search, I rewrote the function into this with ChatGPT and community search:

const results = [];
mapBeats.data.forEach((value, index) => {
  const result = query6_lambda.trigger();
  results.push(result);
});
return results;

But it still does not wait until the first loop ends and runs the second; it calls secondary workspace 6 times at once. So maybe the problem is that the loop does not wait for the workflow response status? Because my secondary workflow execution time is like 20 seconds.

I also tried await but it's not working. Any ideas?

Hi @snake302,

If you need to wait for each run to finish, then you do need to use promises. Try the following:

const results = [];
for (const [index, value] of mapBeats.data.entries()) {
  const result = await query6_lambda.trigger();
  results.push(result);
});
return results;

That should give you the same loop process as you've described above, but that await keyword tells it not to proceed until the promise (which is what query6_lambda.trigger() returns) resolves. If you wanted them to all run at the same time, you would just need to change the last line to return await Promise.all(results) because results would be an array of promises.

Hey @mikecb,

I really appreciate your response, and I tried your code with await, but it still executes without waiting until they finish. Maybe there's something wrong with my workspace?


Screenshot 2024-03-06 at 14.46.47

Hi @snake302,

Very odd. I just tried to replicate it and it worked. This is my flow:

Looking at your logs, it looks like it's working. I'd go into your secondary workflow and look at the job history to ensure you are sending the right data back via the response. Also, a mistake I've made a few times, is forgetting to hit deploy after making changes while working on connected workflows. So double check both workflows have the latest version deployed, and see what the response on the second one looks like in the logs. Feel free to post some screenshots if that doesn't solve it.

1 Like

yea, you are absolutely right! Thank you so much! This is helped me!

I forgot to add response block and the flow were retuning just pure js code like

return query.data

with 200 response block everything is working fine!

1 Like