The problem of clock function in Generator function in ES6

var clock = function*() {
  while (true) {
    console.log("Tick!");
    yield;
    console.log("Tock!");
    yield;
    }
};

you can see that the state machine implemented by the Generator function does not need to set initial variables or switch states. Compared with the ES5 implementation, the above Generator function implementation shows that the external variable ticking, used to save the state is less, so it is more concise, more secure (the state will not be illegally tampered with), more in line with the idea of functional programming, and more elegant in writing. Generator can save state without external variables because it itself contains the first state and the second state.


Mar.10,2021

at least post a context or link.

The first and second states of

refer to the value of the variable ticking , first true and then false
, because the clock function is gone after execution, and an external variable is needed to ensure that the previous state can be correctly obtained in the next execution. While Generator does not need it, its internal mechanism ensures that it can get the information of the previous state.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bf12b5-31d74.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bf12b5-31d74.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?