Placing a secondary counter for break on possibily (long) loops on development / testing

I was learning about generators in JavaScript and ran some code I wrote to test. Unfortunately it locked up my FireFox and I had to killall firefox on my machine in terminal.

If you're testing something and you are not sure when or how a loop is going to end, then it would be wise to place a secondary counter and break the loop when it reaches 100 or 500 etc so that it doesn't freeze your browser. (In this case this is a JavaScript file and called in an HTML file)

function* demo()
{
    let value1 = 11;
    let value2 = 22;
    let value3 = 33;
    let value4 = 44;
    let value5 = 55;

    yield value1;
    yield value2;
    yield value3;
    yield value4;
    yield value5;
}

let generateObj = demo();
let g = generateObj.next();
while (g.value !== undefined)
{    
    console.log(g.value);    
}

This code will keep printing 11 because the loop is infinite as there is a missing g = generateObj.next(); in the while loop.

So have a counter to break when it reaches a max threshold of iterations.

Place a counter in the loop and break if it keeps printing the same value or some other non-sensible value.

let g = generateObj.next();
let max = 100, i = 0;
while (g.value !== undefined)
{    
    console.log(g.value);
    if (i++ > max) break;
}

Anyhow, the correct code should be :

let g = generateObj.next();
let max = 100, i = 0;
while (g.value !== undefined)
{    
    console.log(g.value);
    if (i++ > max) break;
    g = generateObj.next();
}

Full code : (I used nodejs to run this instead of using the browser)

/*
$ node generators-iterable.js 
11
22
33
44
55
*/
function* demo()
{
    let value1 = 11;
    let value2 = 22;
    let value3 = 33;
    let value4 = 44;
    let value5 = 55;

    yield value1;
    yield value2;
    yield value3;
    yield value4;
    yield value5;
}

let generateObj = demo();
let g = null;
let max = 100, i = 0;
while ((g = generateObj.next().value) !== undefined)
{
    console.log(g);
    if (i++ > max) break;
}