Ben :
The following JavaScript causes the runtime to hang on Chrome (v80.0.3987.116) and Firefox (v72.0.2) on OSX 10.15.2.
Why?
Note that I am marking the iterator function as async
.
const iterable = {
async *[Symbol.iterator]() {
yield 'one'
}
}
console.log([...iterable])
Bergi :
Because now your [Symbol.iterator]
method no longer returns an Iterator, it does return an AsyncIterator. That's still an object with a .next()
method, so the spread syntax trying to iterate it doesn't complain. But the calls to the .next()
method do no longer return {value:…, done:true}
objects, they always return a promise. These promise objects have no truthy done
property, so your iterator never stops…
You can achieve the same result with
const iterable = {
*[Symbol.iterator]() {
while (true) {
yield
}
}
}
console.log([...iterable])
or
const iterable = {
[Symbol.iterator]() {
return {
next() {
return {};
}
}
}
}
console.log([...iterable])