function* hanoi(n, source, target, spare) { if (n === 0) { return; } yield* hanoi(n - 1, source, spare, target); yield { disk: n, from: source, to: target }; yield* hanoi(n - 1, spare, target, source); } // Nothing runs until a move is asked for, so the caller sets the pace. const moves = hanoi(3, "A", "C", "B"); for (const { disk, from, to } of moves) { console.log(`Move disk ${disk} from ${from} to ${to}`); }