jsfiddle / jsfiddle-users

Discussions, issues and docs for JSFiddle.
https://jsfiddle.net
314 stars 114 forks source link

JSFiddle Skips Synchronous Code #1874

Closed AlexanderKidd closed 3 months ago

AlexanderKidd commented 4 months ago

I have a button to run a main() function, and inside the main function is a while loop that goes for a certain number of iterations.

The issue is the part OUTSIDE the while loop (after it) is getting triggered as soon as the first while loop iteration is done.

Really bizarre, I don't believe plain JS is supposed to do that and I've double-checked my encapsulation in brackets.

Pseudocode:

function calcWinner() {
}

function main() {
  ITERATIONS = 5000;

  while ( i < ITERATIONS ) {
     forEach( playGame );

     if( etc) calcWinter();
  }

  console.log("This is printing out immediately");
  saveAs("save to file from File.js API");
}

main()
AlexanderKidd commented 4 months ago

Edit: It seems to be working (and blazing fast for this training model) by turning off the Beta Console for now.

I also have things like Live Code Validation and Autosave/Autorun off so that might help too.

Probably just some very odd synchronicity issues going on with console mostly?

oskarkrawczyk commented 3 months ago

Changing your code to something that works:

function main() {
  ITERATIONS = 10
  let i = 0

  while ( i < ITERATIONS ) {
    console.log(i)
    i++;
  }

  console.log("This is printing out immediately");
}

main()

This code behaves exactly how I'd expect:

Screenshot 2024-08-06 at 10 58 35

Tested in JSFiddle, native browser console, other playgrounds (MDN, etc). All have the same result. Having our UI Console turned on doesn't seem to change anything for me.

If you have more details, and can post a working example of it not working as expected please let me know. Closing for now since it seems to be invalid.