You are reading a single comment by @Raik and its replies. Click here to read the full conversation.
  • Hi,
    I am not JS expert, but I think it all comes down to the way the Web IDE handles your script as @fanoush already mentioned. The code gets executed line by line as it gets uploaded. You can clearly see this when you hit upload, then change to the left side of the editor and hit the up arrow button to show the executed command history.

    Here is your first example with added timestamps:

    setTimeout( function(){console.log("nope "+getTime());},0);
    console.log("yep "+getTime());
    console.log("yep "+getTime());
    console.log("yep "+getTime());
    console.log("yep "+getTime());
    console.log("yep "+getTime());
    var dontgetit = 345;
    

    Output:

    >nope 1592558510.36899995803
    yep 1592558510.37299990653
    >yep 1592558510.37599992752
    >yep 1592558510.38100004196
    >yep 1592558510.38400006294
    >yep 1592558510.39000010490
    

    You can see that nope was written earlier than the yeps. Now let's wrap this in a function and call the function to execute it as a single block:

    function test() {
     setTimeout( function(){console.log("nope "+getTime());},0);
      console.log("yep "+getTime());
      console.log("yep "+getTime());
      console.log("yep "+getTime());
      console.log("yep "+getTime());
      console.log("yep "+getTime());
      var dontgetit = 345;
      return dontgetit;
    }
    
    print(test());
    

    Output:

    >yep 1592559009.20099997520
    yep 1592559009.20099997520
    yep 1592559009.20199990272
    yep 1592559009.20300006866
    yep 1592559009.20300006866
    345
    nope 1592559009.20700001716
    

    You see that the nope now is being executed last, even after printing the result of the function. Maybe you can run your other tests in a similar fashion and it will clear things up for you.

About

Avatar for Raik @Raik started