0

EDIT: As someone pointed out, this has to do with the asynchronous nature of the console, and an answer can be found here: Weird behavior with objects & console.log

I have an issue where JS will correctly detect an iterator, yet will not be able to directly access its properties: I know this because I can console.log() the iterator, which will correctly return a non-empty result with size > 0, yet if I console.log() the iterator.size, I get 0. There are no error messages.

Here, listOfLabelLists is of type Set<string>[], although I've tried converting its elements from sets to maps and arrays, with the same result: I can log labelList correctly, but logging just its size/length property returns 0, and so the code (commented below) is never read.

listOfLabelLists.forEach(labelList => {

//console.log(labelList) returns a set with size 7.
//console.log(labelList.size) returns 0.

  labelList.forEach(wordLabel => {//This never runs because the set is interpreted as empty.
    ...
  })
})

I wrote this code in TypeScript, so I wondered if it was compiling incorrectly. However, after running tsc on the file to output the JS formatted version, it looks like TypeScript is not at fault here, as the relevant JS portion was identical.

Brian
  • 1
  • 1
  • Iterators don't have a `size` or `length` or anything like that. You seem to be speaking about the base iterable here. Also, it's not at all clear what and how you're trying to iterate over. As a guess, if you're seeing this in the browser, beware oh how the console displays live objects. See [Weird behavior with objects & console.log](https://stackoverflow.com/q/23429203) and [Is Chrome's JavaScript console lazy about evaluating arrays?](https://stackoverflow.com/q/4057440) – VLAZ Jul 09 '21 at 23:09
  • @VLAZ Thanks vlaz, that seemed to be the problem! And yes, sorry for the terminology, I meant the iterables. – Brian Jul 10 '21 at 14:04

0 Answers0