(We're talking about this thing)

function* myCoolGenerator() {}

Things that generator functions won't do for you

  • Make your app magically faster
  • Earn you blessings from the modern JavaScript fairies
  • Help you win friends and influence people

Things you may have heard about generator functions which are true but not very helpful

  • Generators are functions that you can exit and re-enter later
  • You can use a generator function to implement a finite state machine
  • They help you impress your friends and family

What are generators Functions?

A generator function creates a Generator!

What are generators, actually?

Exactly the same as Java Iterables or C# Enumerables!!!

Basic generator function

function* myCoolGenerator(limit) { // can have 0 or more inputs
  for (let i = 1; i <= limit; i++) {
    yield i; // produces a value
  }
  
  return; // signals that the generator is done
}

for (let element of myCoolGenerator(10)) {
  console.log(element);
}

// prints 1, 2, 3, ... , 10

the main idea of generators!!!

"Generators allow you to define a sequence of elements and process each element as it's produced rather than waiting until the entire sequence is produced"

But how is that useful?

examples!!!!

Further Reading

Made with Slides.com