Quick actions

cmd+k|ctrl+k

Navigation

Languages

Generators

Snippet info

Language

JavaScript

Visibility

public

Author

nvmarcosalex

Created

2018-08-22T20:57:41Z

Updated

2018-08-23T13:17:34Z

// function* getEmployee() {
//     console.log('the function has started');

//     const names = ['Amanda', 'Diego', 'Farrin', 'James', 'Kagure', 'Kavita', 'Orit', 'Richard'];

//     // for (const name of names) {
//     //     console.log( name );
//     //     yield;
//     // }
    
//     for (const name of names) {
//         yield name;
//     }

//     console.log('the function has ended');
// }


// const generatorIterator = getEmployee();
// generatorIterator.next();
// generatorIterator.next();
// generatorIterator.next();

// console.log(generatorIterator.next());


// function* udacity() {
//     console.log('Step 1');
//     yield 'Richard';
//     console.log('Step 2');
//     yield 'James'
//     console.log('End function');
// }

// const teste = udacity();

// console.log(teste.next());
// console.log(teste.next());
// console.log(teste.next());


// function* displayResponse() {
//     const response = yield;
//     console.log(`Your response is "${response}"!`);
// }

// const iterator = displayResponse();


// iterator.next(); // starts running the generator function
// iterator.next('Hello Udacity Student'); // send data into the generator
// the line above logs to the console: Your response is "Hello Udacity Student"!


function* getEmployee() {
    const names = ['Amanda', 'Diego', 'Farrin', 'James', 'Kagure', 'Kavita', 'Orit', 'Richard'];
    const facts = [];

    for (const name of names) {
        // yield *out* each name AND store the returned data into the facts array
        facts.push(yield name); 
    }

    return facts;
}

const generatorIterator = getEmployee();

// get the first name out of the generator
let name = generatorIterator.next().value;

// console.log(generatorIterator.next()); // Amanda
// console.log(generatorIterator.next()); // Diego
// console.log(generatorIterator.next()); // Farrin
// console.log(generatorIterator.next()); // James
// console.log(generatorIterator.next()); // Kagure
// console.log(generatorIterator.next()); // Kavita
// console.log(generatorIterator.next()); // Orit
// console.log(generatorIterator.next()); // Richard
// console.log(generatorIterator.next());

// pass data in *and* get the next name
name = generatorIterator.next(`${name} is cool!`).value; 

// pass data in *and* get the next name
name = generatorIterator.next(`${name} is awesome!`).value; 

// pass data in *and* get the next name
name = generatorIterator.next(`${name} is stupendous!`).value;

// you get the idea
name = generatorIterator.next(`${name} is rad!`).value; 
name = generatorIterator.next(`${name} is impressive!`).value;
name = generatorIterator.next(`${name} is stunning!`).value;
name = generatorIterator.next(`${name} is awe-inspiring!`).value;

// pass the last data in, generator ends and returns the array
const positions = generatorIterator.next(`${name} is magnificent!`).value; 

console.log(positions.join('\n'));










INFO