Generators are cool ways to have infinitely running functions #shorts

preview_player
Показать описание
Showing you how to use a generator to create a counter.

------------

Рекомендации по теме
Комментарии
Автор

Doesn't setting up a closure do exactly the same, like nesting functions?

anthonyowolabi
Автор

This reminds me of signal. What is the difference?

ooker
Автор

I find that the `.next().value` syntax to be too unwieldy. In my recent project, I turned my generator to an object with a getter. Something like...

const Counter = {
count: 0,
get nextCount() {
return this.count++
},
}

Counter.nextCount // 0
Counter.nextCount // 1
Counter.nextCount // 2

Then you could probably create a method that resets the counter, or turn this into a full-fledged class.

re.liable
Автор

Using counter as a global variable outside i can use it as some state that many other properties depend on how can i replace that with a generator?

oluwatomisinbabatunde
Автор

random thing: you can also use while(;;) instead of while(true)

RAIN_BT
Автор

And what's the benefit of the generator?

SirSidi
Автор

Generators are awesome and very versatile. It's sad that they are nearly impossible to implement without compiler support.

redcrafterlppa
Автор

Any ways that you have used this? In a project etc

danielchettiar