Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

generators in javascript

function* g(){    //or function *g(){}
  console.log("First");
  yield 1;
  console.log("second");
   yield 2;
  console.log("third");
} 
let generator=g();
generator.next();
generator.next();
Comment

generators in javascript

//- With a generator function, values are not evaluated 
//until they are needed.
//- Therefore a generator allows us to define a potentially
//infinite data structure.

function* generator() {
  yield 1;
  yield 2;
  yield 3;
}

const gen = generator(); // "Generator { }"

console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // 3
Comment

Create JavaScript Generators

// define a generator function
function* generator_function() {
   ... .. ...
}

// creating a generator
const generator_obj = generator_function();
Comment

generator function in javascript

// generator function in javascript
// The function* declaration (function keyword followed by an asterisk) defines a generator function, which returns a Generator object.
function* generator(i) {
  yield i;
  yield i + 10;
}

const gen = generator(10);

console.log(gen.next().value);
// expected output: 10

console.log(gen.next().value);
// expected output: 20

console.log(gen.next().value);
// expected output: undefined
Comment

JavaScript — Generators

function* makeRangeIterator(start = 0, end = 100, step = 1) {
    let iterationCount = 0;
    for (let i = start; i < end; i += step) {
        iterationCount++;
        yield i;
    }
    return iterationCount;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: binary addition javascript 
Javascript :: how to make nodejs more secure 
Javascript :: declare an array nodejs 
Javascript :: javascript random int 
Javascript :: angular 6 key value pair getvalue example 
Javascript :: date format french js 
Javascript :: check in node whether the port is working or not 
Javascript :: javascript compose function 
Javascript :: react native picker 
Javascript :: jquery console log 
Javascript :: document cookies javascript 
Javascript :: leaflet control zoom on scrolling page 
Javascript :: js addeventlistener foreach 
Javascript :: how to add comma in react map 
Javascript :: onfocus js 
Javascript :: Check for a Null or Empty String in JavaScript 
Javascript :: javascript data types 
Javascript :: js copy string to clipboard 
Javascript :: type in javascript 
Javascript :: how to check if a key exists in an object javascript 
Javascript :: js log stack trace 
Javascript :: get ini file with node js 
Javascript :: mongoose search in multiple fields 
Javascript :: ajax actions wordpress 
Javascript :: no special characters express validator 
Javascript :: mdbootstrap react 
Javascript :: angular capitalize pipe 
Javascript :: rich editor react 
Javascript :: javascript run function based on the page size 
Javascript :: simple js drawing program 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =