> For the complete documentation index, see [llms.txt](https://hemantajax-2.gitbook.io/es6-in-depth/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hemantajax-2.gitbook.io/es6-in-depth/new-data-structures/generators.md).

# Generators

* Generators are the function that can be paused and resumed
* Defined using \* after function keyword
* yield to pause a generatore optionally pass back some value
* are used with iterators
* return an iterator when invoked
* next() method resume generator
* can pass value to iterator using next('my value')

**Note**

we have to install "babel-polyfill" to use this feature with webpack

```
// in webpack.config.js add below entry
// entry: ['babel-polyfill', './main.js'],
```

## Generator Demo

```javascript
function* generateMe(){
    yield "One";
    yield "Two";
    yield [1,2,3];
}

var generator = generateMe();
console.log(generator.next());
console.log(generator.next());
console.log(generator.next());
console.log(generator.next());

// Output
// { value="One",  done=false}
// { value="Two",  done=false}
// { value=[3],  done=false}
// { done=true,  value=undefined}
```

## Use generator for dependent API calls

/search/man -> /users/123 -> /photos/233

```javascript
function ajax(url){
    return fetch(url).then(res => res.json()).then((data)=> stepGen.next(data));
}

function* steps(){
    const users = yield ajax('https://reqres.in/api/users');
      console.log(users)
    const user = yield ajax('https://api.github.com/users/hs950559');
      console.log(user);
    const posts = yield ajax('https://reqres.in/api/posts');
      console.log(posts);
}

const stepGen = steps();
  stepGen.next();
```

## Loop over generators ( use for of loop )

```javascript
function* steps(){
    const users = yield 'https://reqres.in/api/users';
    const user = yield 'https://api.github.com/users/hs950559';
    const posts = yield 'https://reqres.in/api/posts';
}

const stepGen = steps();

for(const val of stepGen){
    console.log(val);
}

// output
https://reqres.in/api/users
https://api.github.com/users/hs950559
https://reqres.in/api/posts
```

## Demo - Now i know my abc

```javascript
function* abcGenerator(arr){
    for(var i=0; i<arr.length; i++){
        yield arr[i];
    }
} 

var gen = abcGenerator(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']);

var abc = setInterval(function(){
    var letter = gen.next();

    if(letter.done){
        clearInterval(abc);
        console.log("Now I know my abc");
    } else {
        console.log(letter.value);
    }
}, 500);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hemantajax-2.gitbook.io/es6-in-depth/new-data-structures/generators.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
