Skip to content

isAsyncGenerator

Checks if a value is an async generator object (the result of calling an async function*).

Distinct from isAsyncGeneratorFunction: this predicate targets the instance produced by calling an async generator function, not the function itself.

Available since v2.0.3

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

value is AsyncGenerator<unknown, unknown, unknown> — true if value is an AsyncGenerator instance

Returns true only for the object produced by calling an async function*.

async function* gen() { yield 1; }
isAsyncGenerator(gen())  // => true   (instance)
isAsyncGenerator(gen)    // => false  (function)
isAsyncGenerator([])     // => false

isAsyncGenerator is false for sync generator instances.

function* sync() { yield 1; }
async function* async_() { yield 1; }
isAsyncGenerator(sync())   // => false
isAsyncGenerator(async_()) // => true

View source on GitHub