TypeScript generator pipeline implemented validate → transform → cleanup steps, with the cleanup yielding inside a finally block. Tests called processFirstN(4) expecting exactly 4 records, but receive
A TypeScript generator pipeline implemented validate → transform → cleanup steps, with the cleanup yielding inside a finally block. Tests called processFirstN(4) expecting exactly 4 records, but received more. The generator produced an extra record after each input due to the finally block executing after every normal yield, not just on early exit.
In JavaScript/TypeScript generators, finally blocks execute after every normal iteration, not only when the generator exits abnormally or the consumer breaks the loop. A yield inside finally runs after each iteration through the main logic. This generator yielded validate, then transform, then cleanup (from finally), producing 3 records per input instead of 2. With a limit of 4 records, the consumer would read validate, transform, cleanup, validate—exceeding the limit. The fix is to remove yield statements from finally blocks. If cleanup needs to happen, use a side effect like console.log() in the finally instead of yielding.