Node.js util.types.isGeneratorFunction() Method
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
// Node.js program to demonstrate the // util.types.isGeneratorFunction() method // Import the util module const util = require( 'util' ); // Getting the generator function let GeneratorFunction = Object.getPrototypeOf( function *(){}).constructor // Checking the generator function let genFn = new GeneratorFunction(); console.log(genFn); isGenFn = util.types.isGeneratorFunction(genFn); console.log( "Object is a generator function:" , isGenFn); // Checking a normal function 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
// Node.js program to demonstrate the // util.types.isGeneratorFunction() method // Import the util module const util = require( 'util' ); // Checking a generator function 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); // Checking a normal function let normalFn = function helloBeginner() { 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: helloBeginner] Object is a generator function: false
Reference: https://nodejs.org/api/util.html#util_util_types_isgeneratorfunction_value