Skip to content

isAsyncIterable

Checks if a value implements the async iterable protocol.

Returns true for any object that has a [Symbol.asyncIterator]() method, including async generators. Note that regular iterables (arrays, strings, etc.) are not async iterables.

Available since v2.0.3

import { isAsyncIterable } from '@helpers4/type';
isAsyncIterable(value: unknown): value is AsyncIterable<unknown, any, any>
ParameterTypeDescription
valueunknownThe value to check

value is AsyncIterable<unknown, any, any> — true if value is async iterable

Async generators implement the async iterable protocol.

async function* stream() { yield 1; yield 2; }
isAsyncIterable(stream())   // => true
isAsyncIterable([1, 2, 3])  // => false  (Iterable, not AsyncIterable)
isAsyncIterable('hello')    // => false

Use to type-narrow before consuming a value with for-await-of.

async function consume(source: unknown) {
  if (isAsyncIterable(source)) {
    for await (const item of source) {
      console.log(item);
    }
  }
}

View source on GitHub