DEV Community

Cover image for ES6 - A beginners guide - Generators

ES6 - A beginners guide - Generators

Stefan Wright on December 01, 2021

Forewarning: this topic is hard! It's taken me a week to even start to get my head around it. You may have to read this a few times also, I have sp...
Collapse
 
jamelleboose profile image
Jamelle Boose

I don’t understand why in the first 2 console log examples, value: 3 gets printed twice.

Collapse
 
stefanwrightcodes profile image
Stefan Wright

that'll just be a typo :) I'll get that fixed

Collapse
 
jamelleboose profile image
Jamelle Boose

You had one job! 😂😂😂 Jk, I was so confused though. Thanks for confirming!

Thread Thread
 
stefanwrightcodes profile image
Stefan Wright

Haha I know, I failed haha

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Can also be used with for...of:

function * numberGenerator() {
   yield 101
   yield 42
   yield 33
}

for (i of numberGenerator()) {
   console.log(i)
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
stefanwrightcodes profile image
Stefan Wright

Of course! I knew there was an asepct I missed out in my write up!!

Collapse
 
guillep2k profile image
Guillermo Prandi

Example use cases: 1) checksum calculation, 2) deliver content as it arrives (eg. from a server call), 3) break up collections in an efficient way, etc.

Collapse
 
stefanwrightcodes profile image
Stefan Wright

Great suggestions, I might have to look into some of them for further examples

Collapse
 
shadowruge profile image
izaias

Quão maravilhoso e aprender e com pessoas criativas o aprendizado fica melhor a cada artigo.
nota
const nota =()=>{
let number = '1000'
console.log(number, ('Parabéns'))
}

Collapse
 
opeolluwa profile image
ADEOYE ADEFEMI OPEOLUWA

That's a huge one thanks 😊

Collapse
 
stefanwrightcodes profile image
Stefan Wright

This concept is sooo confusing but if its helped someone then I feel that I must have learnt something :)

Collapse
 
tobiasfranek profile image
Tobias Franek

Awesome Post! Thank you.