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

Node.jsServer Side ProgrammingProgramming

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

Syntax

util.types.isGeneratorFunction(value)

Parameters

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

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

Example 1

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

// util.types.isGeneratorFunction() Demo Example

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

// Passing a normal function
console.log("1." + util.types.isGeneratorFunction(function foo() {}));

// Passing a generator function
console.log("2." + util.types.isGeneratorFunction(function* gen() {}));

Output

C:\home\node>> node isGeneratorFunction.js
1.true
2.false

Example 2

// util.types.isGeneratorFunction() Demo Example

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

var gen = function* id() {
   var index = 0;
   while (true)
      yield index++;
}

// Passing a generator function
console.log("1." + util.types.isGeneratorFunction(gen));

var fn = function f() {
   console.log('Welcome to TutorialsPoint !')
}

// Passing a normal function
console.log("2." + util.types.isGeneratorFunction(fn));

Output

C:\home\node>> node isGeneratorFunction.js
1.true
2.false
raja
Published on 17-Aug-2021 11:53:20
Advertisements