Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!