Node.js util.types.isGeneratorObject() Method
Last Updated :
12 Oct, 2021
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:
const util = require( 'util' );
let GeneratorFunction =
Object.getPrototypeOf( function *(){}).constructor
let genFn = new GeneratorFunction();
let genObj = genFn();
console.log(genObj);
isGenObj = util.types.isGeneratorObject(genObj);
console.log( "Object is a generator object:" , isGenObj);
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:
const util = require( 'util' );
let genFn = function * generateNumber() {
let id = 0;
while ( true )
yield id++;
};
let genObj = genFn();
console.log(genObj);
isGenObj = util.types.isGeneratorObject(genObj);
console.log( "Object is a generator object:" , isGenObj);
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
Share your thoughts in the comments
Please Login to comment...