Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
Verify Iterator versus AsyncIterator type

Is there any known trick in JavaScript to tell the difference between Iterator and AsyncIterator, without triggering iteration?

I'm trying to implement the following type checker:

function isAsyncIterator<T>(i: Iterator<T> | AsyncIterator<T>): boolean {
    // returns:
    //  - true, if i is an asycnronous iterator
    //  - false, if i is a syncronous iterator
}

I know that calling next would tell us so, but I need it at the point when I cannot trigger iteration.

Also, even though I gave the example in TypeScript, I need to check it strictly at run-time.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This is not possible. Iterators are defined by a protocol, not by a tangible property, so due to the halting problem one cannot determine whether an object is an iterator without actually iterating it. See also the similar question What is the technical definition of a Javascript iterable and how do you test for it?.

However, there are good heuristics that will detect most well-behaved iterator implementations and all builtin iterators:

  • Iterators typically are iterable (they couldn't be used in for … of loops otherwise, limiting their usefulness) and will delegate to themselves for that. So

    function isIterator(obj) {
        if (Object(obj) !== obj) return false;
        const method = obj[Symbol.iterator];
        if (typeof method != 'function') return false;
        const iter = method.call(obj);
        return iter === obj;
    }
    function isAsyncIterator(obj) {
        if (Object(obj) !== obj) return false;
        const method = obj[Symbol.asyncIterator];
        if (typeof method != 'function') return false;
        const aIter = method.call(obj);
        return aIter === obj;
    }
    
  • Iterators inherit from the builtin %IteratorPrototype%. Until the iterator helpers proposal will introduce the global Iterator constructor these objects are a bit cumbersome to access and test for, but it's possible nonetheless.

    const GeneratorPrototype = Object.getPrototypeOf(function*() {}).prototype;
    const IteratorPrototype = Object.getPrototypeOf(GeneratorPrototype);
    const AsyncGeneratorPrototype = Object.getPrototypeOf(async function*() {}).prototype;
    const AsyncIteratorPrototype = Object.getPrototypeOf(AsyncGeneratorPrototype);
    
    function isIterator(obj) {
        return IteratorPrototype.isPrototypeOf(obj);
    }
    function isAsyncIterator(obj) {
        return AsyncIteratorPrototype.isPrototypeOf(obj);
    }
    

    (Of course, like any instanceof check, this doesn't work with objects from other realms, which is why I would recommend the first approach)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda