Is there a way to access the final value of the iterator in a finally block in a generator?
function*generator() {
try {
let value1 = yield(1)
let value2 = yield(2)
} finally {
console.log("Ending, how to access the result? (4)")
} }
var iterator = generator()
var result1 = iterator.next(1)
var result2 = iterator.return(4)
No, it is not possible to access the argument passed to
.return()
inside the generator. It's the same as areturn
statement inside thetry
block - you can only overwrite the return value in thefinally
clause, but not read it.