Node.js – util.types.isGeneratorObject() Method

Node.jsServer Side ProgrammingProgramming

The util.types.isGeneratorObject() method checks whether the passed value is a built-in generator object or not. If the above condition is satisfied, it returns True, else False. The return value may differ from the original source code if a transpilation tool is used.

Syntax

util.types.isGeneratorObject(value)

Parameters

  • value − This input value takes input for the required parameter and checks if it's a Generator object or not.

It returns True or False based upon the input value passed.

Example 1

Create a file "isGeneratorObject.js" and copy the following code snippet. After creating the file, use the command "node isGeneratorObject.js" to run this code.

// util.types.isGeneratorObject() Demo Example

// Importing the util module
const util = require('util');

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

const gen = generator();

// Passing the generator object
console.log("1." + util.types.isGeneratorObject(gen));

// Passing the generator function
console.log("2." + util.types.isGeneratorObject(generator()));

Output

C:\home\node>> node isFloat32Array.js
1.true
2.true

Example 2

// util.types.isGeneratorObject() Demo Example

// Importing the util module
const util = require('util');

// Defining a generator function
function* infinite() {
   let index = 0;

   while (true) {
      yield index++;
   }
}

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

console.log(generator.next().value); // 0
console.log(generator.next().value); // 1
console.log(generator.next().value); // 2

// Passing the generator object
console.log("1." + util.types.isGeneratorObject(generator));

// Passing the generator next-value function
console.log("2." + util.types.isGeneratorObject(generator.next()));

Output

C:\home\node>> node isFloat32Array.js
0
1
2
1.true
2.false
raja
Published on 17-Aug-2021 11:33:31
Advertisements