You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • @user114097, Works as expected... if you know what is going on...

    EDIT: missed somehow @Raik 's post... so keeping goin is TL;DR, except checking out the linked conversation in the bottom of the post...

    All this excitement for just nothing other than that the (Chrome) browser console (and nodeJS console) behave a little bit different than the Espruino console. Btw, ECMAScfript / ECMA-262 / ES # makes no specification about console implementation. In other words, your statement If its not a javascript standard then we can forget about it. is a bit off-track.

    I want to tackle this difference first and then move on to a more essential thing...

    The Espruino console and related REPL work line by line - just as @fanoush pointed out - event though you may have pasted all seven (7) code lines in post #1 at once, where as the browser console - if you copy it all at once - behaves as expected. The (Chrome) browser console works like uploading of JavaScript in a Web page: Wait until all code that is between the tag is received, then it is compiled into byte code, and then it is executed. For understandable memory reason and because Espruino interprets from source and does not compile and execute from byte code, it does this line wise - more precisely - statement wise execution.

    Now you are asking yourself what happens when I upload this code onto Espruino...

    This brings me to my almost most famous and over and over repeated statement in tis forum:

    Do not put active stuff - other than function / prototype / class definitions into code level 0 or root level.

    ...since it is just the same as pasting that very same code into the console. Why? The upload uses the very same REPL 'channel' to get the code onto the Espruino board where the Espruino JavaScript interpreter receives it and executes it line by line. And now my citation continues:

    *** Always put your code into functions and activate the execution in the onInit() function.***

    While you develop and upload over and over this is a little bit a pain, so add as last line something that triggers onInit() deferred with a timeout. Why? Because you do not want to interfere with the upload!

    As a side note why you do not see the uploading of the code in the console? ...because the echo is turned off before the upload and turned back on after it.

    The code then looks like this:

    function f() {
      setTimeout( function(){console.log("nope");},0);
      console.log("yep");
      console.log("yep");
      console.log("yep");
      console.log("yep");
      console.log("yep");
      var dontgetit = 345;
    }
    
    function onInit() {
      f();
    }
    
    setTimeout(onInit,999); // while dev'g; remove before upload for save
    

    With that code you get what you expect...

     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v05 (c) 2019 G.Williams
    >
    yep
    yep
    yep
    yep
    yep
    nope
    > 
    

    Happy Espruino coding! - ao

    PS: @user114097, more? See this conversation: simple explanation how to save code that espruino run on start?. - I guess it is my most referenced one.

About

Avatar for allObjects @allObjects started