📜  Node.js util.types.isGeneratorFunction() 方法

📅  最后修改于: 2022-05-13 01:56:36.572000             🧑  作者: Mango

Node.js util.types.isGeneratorFunction() 方法

util.types.isGeneratorFunction() 方法是 util 模块的内置应用程序编程接口,主要用于支持 Node.js 自身内部 API 的需求。
util.types.isGeneratorFunction() 方法用于检查给定值是否是生成器函数。

句法:

util.types.isGeneratorFunction( value )

参数:此函数接受一个如上所述和如下所述的参数:

  • value:它是检查生成器函数的值。

返回值:它返回一个布尔值,即如果传递的值是生成器函数,则返回 true,否则返回false

下面的程序说明了 Node.js 中的util.types.isGeneratorFunction()方法

示例 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);


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 helloGeeks() { 
      console.log("Hello World")
};
  
console.log(normalFn);
  
isGenFn = util.types.isGeneratorFunction(normalFn);
console.log("Object is a generator function:", isGenFn);


输出:

[GeneratorFunction: anonymous]
Object is a generator function: true
[Function: helloWorld]
Object is a generator function: false

示例 2:

节点.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 helloGeeks() { 
      console.log("Hello World")
};
  
console.log(normalFn);
  
isGenFn = util.types.isGeneratorFunction(normalFn);
console.log("Object is a generator function:", isGenFn);

输出:

[GeneratorFunction: getID]
Object is a generator function: true
[Function: helloGeeks]
Object is a generator function: false

参考: https://nodejs.org/api/util.html#util_util_types_isgeneratorfunction_value