Search code examples
javascriptfirefoxfirefox-developer-tools

Firefox debugger pauses in the wrong place on errors coming from functions stored in arrays


Let's say I have the following code:

const myFunctions = [];
function callMyFunctions(v) {
    for(let func of myFunctions) {
        func(v);
    }
}

myFunctions.push((v) => {
    console.log("Foo", v);
    throw "Error!";
});

callMyFunctions(123);

If I run this code on Firefox with the "Pause on exceptions" box checked in the Firefox developer tools, the debugger will pause in the incorrect place. Instead of pausing on the throw "Error!" line, it pauses on the func(v) line. If I then unpause the execution, the traceback written to the console does point to the correct place, but that's much less useful than having it pause there mid-execution.

This issue does not occur if I don't store the function in an array. The following code has the debugger pausing in the correct place:

let myFunction = null;
function callMyFunction(v) {
    myFunction(v);
}

myFunction = (v) => {
    console.log("Foo", v);
    throw "Error!";
}

callMyFunction(123);

Is there any way for me to get Firefox to pause in the correct place for errors emanating from functions stored in arrays?


Solution

  • Oddly enough, if I access the array via its indexes rather than an iterator, the debugger works correctly.

    const myFunctions = [];
    function callMyFunctions(v) {
        // The following two options both work correctly:
        for(let i = 0; i < myFunctions.length; i++) {
            myFunctions[i](v);
        }
        // And
        for(let i in myFunctions) {
            myFunctions[i](v);
        }
    }
    
    myFunctions.push((v) => {
        console.log("Foo", v);
        throw "Error!";
    });
    
    callMyFunctions(123);