Checking whether something is iterable

后端 未结 8 949
甜味超标
甜味超标 2020-11-28 04:55

In the MDN docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

The for...of construct is described to be able to

8条回答
  •  情书的邮戳
    2020-11-28 05:22

    For async iterators you should check for the 'Symbol.asyncIterator' instead of 'Symbol.iterator':

    async function* doSomething(i) {
        yield 1;
        yield 2;
    }
    
    let obj = doSomething();
    
    console.log(typeof obj[Symbol.iterator] === 'function');      // false
    console.log(typeof obj[Symbol.asyncIterator] === 'function'); // true
    

提交回复
热议问题