This code demonstrates the how to combine a reading generator and a writing generator together into a duplex generator.
The duplex generator will simultaneously read and write at the same time from both the readable and writable generator.
Throwing an exception throws it to the writable generator, but we continue on (but make sure to skip 1 iteration by using the errored
flag).
We use the null
value to indicate the ending of the generator, it ends both reading and writing sides.
Running gives:
START READ
START WRITE: A
FINISH READ
FINISH WRITE: A
{ value: { value: 'READ: 1', done: false }, done: false }
GOT AN ERROR
START READ
START WRITE: B
FINISH READ
FINISH WRITE: B
{ value: { value: 'READ: 2', done: false }, done: false }
START READ
START WRITE: C
FINISH READ
FINISH WRITE: C
{ value: { value: 'READ: 3', done: false }, done: false }
END READ
END WRITE
END DUPLEX
{ value: undefined, done: true }