You are reading a single comment by @Unreality and its replies. Click here to read the full conversation.
  • The blocks "After n seconds do" and "Every n seconds do" are suited for the task, like this:

    Every 2 seconds do
    ----After 1 seconds do
    --------print "LED 1 off"
    ----print "LED 2 on"
    

    And yes, it first prints "LED 1 on" and then "LED 1 off", and that makes complete sense. If you tell someone to switch the light out after 1 minute from now on, and then to switch the lights on immediately the exact same thing will happen: Lights on first, then lights off. So I'd say even kids can understand this.

    Now duplicate the block, multiply the numbers by 2.5 and update the text. The kids will see the real power of async programming. You could toggle one LED at 1 Hz and another one at 2.5 Hz. How would you do this with a while loop using waits?

    The wait block should be removed for Espruino as it doesn't do anything useful, Javascript being an async progamming language.

  • being an async progamming language.

    do you mean javascript ES7 which supports async / await changed the nature of language?

  • No, it's still async. In Espruino you can take the block "Wait BTN1 both edge do", that is basically the same as an await. And ofc the "sleep" functions implemented in ES7 are still async, if there's any function triggered while a "sleep" function is "active" that function will be executed.
    The give an example:
    -You have the LED "toggle" function, switching an LED on and off every 1 second, either with setInterval() or with a ES7 "sleep" function.
    -You have another "fancy" function to do some fancy stuff, taking 3 seconds.
    Whenever the "fancy" function is executed your LED will stay on (or off) for 3..4 seconds, because the "toggle" function is only executed after the "fancy" function has completed, because there's no multitasking and things run async.

About

Avatar for Unreality @Unreality started