Open In App

Node.js util.types.isGeneratorObject() Method

Last Updated : 12 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The util.types.isGeneratorObject() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs.
The util.types.isGeneratorObject() method is used to check if the given value is a generator object or not.

Syntax:

util.types.isGeneratorObject( value )

Parameters: This function accepts a single parameter value which holds the value that would be checked for a generator object.

Return Value: It returns a boolean value i.e. true if the passed value is a generator object otherwise returns false.

Below programs illustrate the util.types.isGeneratorObject() method in Node.js:

Example 1:




// Node.js program to demonstrate the
// util.types.isGeneratorObject() method
   
// Import the util module
const util = require('util');
   
// Creating a generator function
let GeneratorFunction = 
    Object.getPrototypeOf(function*(){}).constructor
  
let genFn = new GeneratorFunction();
   
// Checking the generator object
let genObj = genFn();
console.log(genObj);
   
isGenObj = util.types.isGeneratorObject(genObj);
console.log("Object is a generator object:", isGenObj);
   
// Checking a normal object
normalObj = {a: "1", b: "2"};
console.log(normalObj);
   
isGenObj = util.types.isGeneratorObject(normalObj);
console.log("Object is a generator object:", isGenObj);


Output:

Object [Generator] {}
Object is a generator object: true
{ a: '1', b: '2' }
Object is a generator object: false

Example 2:




// Node.js program to demonstrate the
// util.types.isGeneratorObject() method
   
// Import the util module
const util = require('util');
   
// Creating a generator function
let genFn = function* generateNumber() {
    let id = 0;
      
    while (true)
        yield id++;
};
   
// Checking the generator object
let genObj = genFn();
console.log(genObj);
   
isGenObj = util.types.isGeneratorObject(genObj);
console.log("Object is a generator object:", isGenObj);
   
// Checking a normal object
normalObj = {arg1: "1", arg2: "2"};
console.log(normalObj);
   
isGenObj = util.types.isGeneratorObject(normalObj);
console.log("Object is a generator object:", isGenObj);


Output:

Object [Generator] {}
Object is a generator object: true
{ arg1: '1', arg2: '2' }
Object is a generator object: false

Reference: https://nodejs.org/api/util.html#util_util_types_isgeneratorobject_value



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads