Skip to content

isGeneratorFunction

Checks if a value is a generator function (a function* declaration or expression).

Distinct from isGenerator: this predicate targets the function itself, not the iterator it produces when called.

Available since v2.0.3

import { isGeneratorFunction } from '@helpers4/guard';
// or, from the all-in-one package (same code, one install):
import { isGeneratorFunction } from 'helpers4/guard';
isGeneratorFunction(value: unknown): value is GeneratorFunction
ParameterTypeDescription
valueunknownThe value to check

value is GeneratorFunction — true if value is a GeneratorFunction

Returns true for function* declarations and expressions.

function* gen() { yield 1; }
isGeneratorFunction(gen)      // => true
isGeneratorFunction(gen())    // => false  (instance, not function)
isGeneratorFunction(() => {}) // => false

Filter generator factories from a mixed array

Section titled “Filter generator factories from a mixed array”

Use as a predicate to select only generator functions.

const fns = [() => {}, function* () { yield 1; }, async () => {}];
fns.filter(isGeneratorFunction)
// => [function* () { yield 1; }]

View source on GitHub