Access Generator Result in Finally Block

750 views Asked by At

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)

1

There are 1 answers

0
Bergi On

No, it is not possible to access the argument passed to .return() inside the generator. It's the same as a return statement inside the try block - you can only overwrite the return value in the finally clause, but not read it.