Stop api loop based on a condition - additionalScope

I implemented a loop as is explained in Scripting Retool and it worked perfectly.

But now i need to stop the loop based on a condition.

How can I do that?

This is my loop.

var rows = get_next_page_from_header.value;

function runQuery(i) {
  find_temporary_items_batch.trigger({
    additionalScope: { i: i }, // This is where we override the `i` variable
    // You can use the argument to get the data with the onSuccess function
    onSuccess: function (data) {
      runQuery(i + 1);
    },
  });
}

runQuery(0);

Hi @leticiarezende,

You can use a return statement to stop the query. The example in the docs uses this to stop the query when i >= rows.length to prevent an infinite loop. For example, using the data in the docs, if you wanted to stop running the query if sales is greater than 500.

if (rows[i].sales > 500) {
    console.log("failed check; stopping execution");
    return;
}
1 Like