What are generators in Javascript?

Javascript Generators

Regular functions return only one, single value (or nothing).

Generators can restore (“yield”) numerous qualities, in a steady progression, on-request. They work extraordinary with iterables, permitting to make information streams easily.

Generator functions

To create a generator, It requires a special syntax construct: function*, so-called “generator function”.

function* generateSequence() {
yield 1;
yield 2;
return 3;

Generator function carry on uniquely in contrast to ordinary ones. At the point when such capacity is called, it doesn’t run its code. Rather it restores an uncommon article, called “generator object“, to deal with the execution.

function* generateSequence() {
  yield 1;
  yield 2;
  return 3;

// "generator function" creates "generator object"
let generator = generateSequence();
alert(generator); // [object Generator]

The function code execution hasn’t started yet:

The fundamental technique for a generator is straightaway(). When called, it runs the execution until the closest yield proclamation (worth can be discarded, at that point it’s unclear). At that point the capacity execution stops, and the yielded esteem is gotten back to the external code.

The consequence of next() is consistently an article with two properties:

esteem: the yielded esteem.

done: valid if the capacity code has completed, in any case bogus.

For example, here we make the generator and get its previously yielded esteem:

function* generateSequence() {
  yield 1;
  yield 2;
  return 3;

let generator = generateSequence();

let one = generator.next();

alert(JSON.stringify(one)); // {value: 1, done: false}

As of now, we got the first value only, and the function execution is on the second line:

Let’s call generator.next() again. It resumes the code execution and returns the next yield:

let two = generator.next(); alert(JSON.stringify(two)); // {value: 2, done: false}

And, if we call it a third time, the execution reaches the return statement that finishes the function:

let three = generator.next();

alert(JSON.stringify(three)); // {value: 3, done: true}

Now the generator is done. We should see it from done:true and process value:3 as the final result.

New calls to generator.next() don’t make sense any more. If we do them, they return the same object: {done: true}.


function* f(…) or function *f(…)?

Both syntaxes are correct.

But usually the first syntax is preferred, as the star * denotes that it’s a generator function, it describes the kind, not the name, so it should stick with the function keyword.

Author: Shantun Parmar

55 thoughts on “What are generators in Javascript?

  1. Is Suhagra Otc In Canada bebribiara [url=https://bansocialism.com/]generic cialis 5mg[/url] breallox precio de levitra en farmacias

  2. [url=https://hydroxychloroquinezt.com/]buy plaquenil 100mg[/url] [url=https://pharmacyxonline.com/]grifulvin v 500mg cost[/url] [url=https://sildenafilmeds.com/]viagra price online[/url] [url=https://tadalafilot.com/]where can i buy real cialis[/url] [url=https://gdehealth.com/]zantac for ulcers[/url] [url=https://bestmpills.com/]aceon online[/url] [url=https://zoloftrx.com/]zoloft from india[/url] [url=https://viagratml.com/]buy viagra pharmacy[/url] [url=https://sildenaphil.com/]online generic sildenafil[/url] [url=https://sildenafilpos.com/]viagra 100 buy[/url]

  3. Hi, here on the forum guys advised a cool Dating site, be sure to register – you will not REGRET it [url=https://bit.ly/34nj4Ab]https://bit.ly/34nj4Ab[/url]

  4. [url=https://sildenafilpos.com/]cheap sildenafil[/url] [url=https://ntdrugstore.com/]pletal[/url] [url=https://onlineipharm.com/]best online thai pharmacy[/url] [url=https://cialisperm.com/]generic cialis 20mg pills[/url] [url=https://ivermectin365.com/]stromectol[/url]

  5. [url=http://hpploans.com/]i need cash[/url] [url=http://abcashadvance.com/]best personal loan[/url] [url=http://emflending.com/]credit personal loans[/url] [url=http://instloans.com/]easy money payday loans[/url]

  6. [url=https://sildenafilpos.com/]buy brand name viagra online[/url] [url=https://mntablets.com/]entocort price[/url] [url=https://levitramdf.com/]drug levitra[/url] [url=https://ivermectinforhumans.com/]stromectol nz[/url] [url=https://dexamethasonepill.com/]dexamethasone 1 tablet[/url]

  7. [url=http://aprcashadvance.com/]personal loan poor credit[/url] [url=http://zetapayday.com/]best cash advance loans online[/url] [url=http://loansmt.com/]instant online loans[/url] [url=http://cashadvancemx.com/]online loans instant approval[/url] [url=http://abcashadvance.com/]installment loans online direct lender[/url] [url=http://instloans.com/]how to get cash fast[/url]

  8. [url=http://ipdcash.com/]personal loan interest[/url] [url=http://jeanloans.com/]loans with low monthly payments[/url] [url=http://loansmp.com/]loans online instant approval 5000[/url]

  9. I enjoy you because of all of your efforts on this site. Ellie enjoys doing internet research and it’s easy to see why. All of us hear all concerning the compelling manner you deliver good information by means of the blog and as well cause contribution from other people about this concept plus my daughter is being taught a lot. Enjoy the remaining portion of the new year. You’re the one carrying out a first class job.

  10. Thanks for the recommendations on credit repair on this amazing site. A few things i would tell people is always to give up the actual mentality they can buy today and pay out later. Like a society we all tend to do that for many issues. This includes getaways, furniture, and also items we would like. However, you should separate the wants out of the needs. While you’re working to raise your credit score make some sacrifices. For example it is possible to shop online to save cash or you can visit second hand retailers instead of pricey department stores regarding clothing.

Thanks for your support, You may click on ads to encourage us which assits to writers.

Leave a Reply

Your email address will not be published.