Node.js util.types.isGeneratorFunction() Method
Last Updated :
13 Oct, 2021
The util.types.isGeneratorFunction() 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.isGeneratorFunction() method is used to check if the given value is a generator function or not.
Syntax:
util.types.isGeneratorFunction( value )
Parameters: This function accepts one parameter as mentioned above and described below:
- value: It is the value that would be checked for a generator function.
Return Value: It returns a Boolean value i.e. true if the passed value is a generator function otherwise returns false.
Below programs illustrate the util.types.isGeneratorFunction() method in Node.js:
Example 1:
Node.js
const util = require( 'util' );
let GeneratorFunction =
Object.getPrototypeOf( function *(){}).constructor
let genFn = new GeneratorFunction();
console.log(genFn);
isGenFn = util.types.isGeneratorFunction(genFn);
console.log( "Object is a generator function:" , isGenFn);
let normalFn = function helloWorld() {};
console.log(normalFn);
isGenFn = util.types.isGeneratorFunction(normalFn);
console.log( "Object is a generator function:" , isGenFn);
|
Output:
[GeneratorFunction: anonymous]
Object is a generator function: true
[Function: helloWorld]
Object is a generator function: false
Example 2:
Node.js
const util = require( 'util' );
let genFn = function * getID() {
let id = 0;
while ( true )
yield id++;
};
console.log(genFn);
isGenFn = util.types.isGeneratorFunction(genFn);
console.log( "Object is a generator function:" , isGenFn);
let normalFn = function helloGeeks() {
console.log( "Hello World" )
};
console.log(normalFn);
isGenFn = util.types.isGeneratorFunction(normalFn);
console.log( "Object is a generator function:" , isGenFn);
|
Output:
[GeneratorFunction: getID]
Object is a generator function: true
[Function: helloGeeks]
Object is a generator function: false
Reference: https://nodejs.org/api/util.html#util_util_types_isgeneratorfunction_value
Share your thoughts in the comments
Please Login to comment...