Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda