Can't get Java script loop to Stop

I have a loop running in Java Script and I am trying to stop the loop by comparing to values in localStorage. The code is:

if (localStorage.values.RunCount >= localStorage.values.MaxCount) {
            console.log("No doctors left, exiting loop.");
            break;
        }

I watch the localStorage values as the code progresses. Its supposed to stop at 9 cycles and I can see the RunCount exceeding the MaxCount but the code will not stop.
The console log message never comes out either.
Are there any suggestions?
Mike

1 Like

Hey @mdsmith1 ,
I tested this behavior in Retool and was able to reproduce the same issue on my end.

From my testing, the main reason appears to be that localStorage.values in Retool behaves more like a reactive state object rather than synchronous browser localStorage .

Inside a tight while(true) loop, updates made through localStorage.setValue() are not reflected immediately in localStorage.values . Because of this delayed state synchronization, the loop may continue reading stale values, which prevents the break condition from triggering as expected.

A more reliable approach is to use standard JavaScript variables for the loop logic itself and use localStorage.setValue() only for persistence or UI synchronization.

The following version worked correctly during my testing:

try {
  // Initial values
  let runCount = 0;
  let maxCount = 9;

  // Save initial values
  localStorage.setValue("RunCount", runCount);
  localStorage.setValue("MaxCount", maxCount);

  console.log("Loop started...");

  while (true) {
    console.log(
      `RunCount: ${runCount}, MaxCount: ${maxCount}`
    );

    // Stop condition
    if (runCount >= maxCount) {
      console.log("No doctors left, exiting loop.");
      break;
    }

    // Increment local variable
    runCount++;

    // Update Retool localStorage
    localStorage.setValue("RunCount", runCount);
  }

  console.log("Loop finished.");

  return {
    success: true,
    localStorageValues: {
      RunCount: runCount,
      MaxCount: maxCount,
    },
  };

} catch (error) {
  console.error(error);

  return {
    success: false,
    error: error.message,
  };
}

This correctly returned:

{
  "RunCount": 9,
  "MaxCount": 9
}

Reference screenshot from testing:

So the issue appears to be related to Retool’s reactive state update timing inside synchronous loops, rather than the comparison condition itself.

1 Like

I am having trouble with the line:

localStorage.setValue("MaxCount", MaxCount);

I can see the value of MaxCount in storage is 9 but this line of code retrieves the value as 0. So the program stops right away.

I think if there was a way to get this line to work properly, then the Stop action would work.

Mike

1 Like

I think there are probably two separate issues at play, @mdsmith1. First, modifications to localStorage are asynchronous and should be invoked with await if you need immediate access to localStorage.values.

Second, any changes you make to localStorage from within the code block won't ever be immediately accessible unless you check this option in the query's advanced settings!

1 Like

Darren:

It worked !!!!!!!!! It was the second part of your answer that go it to work.
Thank you so much,
I will mark your answer as the solution
Thanks again.
Mike

2 Likes