Consider the following iterator. It yields strings 'foo' and 'bar' and then returns 'quux' as a return value. I can use Array.from to extract the yields from the iterator but if I do this, I have no way of reading out the return value. The return value is no longer returned by the iterator since Array.from already received (and discarded?) it as part of the protocol.
const iterator = (function* () {
yield 'foo'
yield 'bar'
return 'quux'
})()
const [foo, bar] = Array.from(iterator)
const quux = // ???
The only solution I can think of is writing a spy procedure that monitors the iteration and stores the return value into a predefined variable as a side effect before Array.from discards the return value. Are there better alternative ways of accessing the return value?
let quux
const spy = function* (i) { /* extract return value from i and store it to quux */ }
const [foo, bar] = Array.from(spy(iterator))