-1

What happens is that in my terminal I have a Python that updates the JSON file in real time, causing the file to be open sometimes updating data, prohibiting the JavaScript from accessing, causing the error.

So in this case I'd like that when JavaScript can't access the file, instead of delivering the error, it simply doesn't do anything, to keep showing the value collected before the error.

But as we can see in the code, I'm using try{}catch(e){} and even so the error keeps appearing

enter image description here


function refresh_images() {
    try {
        let selectedstages = document.getElementById("matches").selectedOptions[0].getAttribute("data-stages");
        let selectedevents = document.getElementById("matches").selectedOptions[0].getAttribute("data-events");

        fetch("testedropdown.json")
            .then(response => response.json())
            .then(data => {
                console.log(data.Stages[selectedstages].Events[selectedevents].Eps)
            })
    } catch (e) {
    }
}

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Digital Farmer
  • 1,705
  • 5
  • 17
  • 67
  • 3
    You can't `try`/`catch` the promise unless you _`await`_ it. – jonrsharpe Jul 29 '21 at 16:47
  • 1
    Also, the error is because you receive some malformed JSON. It's best if you fix that. – VLAZ Jul 29 '21 at 16:48
  • Hi @jonrsharpe I honestly didn't understand, could you show me in more detail why in this case it doesn't work and indicate some option for me to use to solve the problem? Thanks in advance! I researched iferror options in Javascript and all indications are to use `try` `catch`! – Digital Farmer Jul 29 '21 at 16:51
  • 1
    *"...could you show me in more detail why in this case it doesn't work..."* - because you're not `await`ing the promise, in an `async` function - *"...and indicate some option for me to use to solve the problem?"* - _do_ `await` it? `.catch` it, if the function can't be `async`? – jonrsharpe Jul 29 '21 at 16:53
  • Hello @VLAZ , the problem is that at my level of knowledge, I can't work around this problem, so Python ends up needing to open the file to update, leaving it inaccessible. So for now I need to find a way to hide the Javascript error. – Digital Farmer Jul 29 '21 at 16:54
  • I will read your link in detail, thank you @jonrsharpe – Digital Farmer Jul 29 '21 at 16:55

1 Answers1

2

fetch performs asynchronous operations and try/catch can only catch errors that are originated when first running the code inside its corresponding block (your error is being thrown on your first .then() callback). You can add a try/catch wrapping response.json() and it would work as you expect.

Alternatively, you can safely reformat your code as follows, leveraging promises catch method:

function refresh_images() {
    const selectedstages = document.getElementById("matches").selectedOptions[0].getAttribute("data-stages");
    const selectedevents = document.getElementById("matches").selectedOptions[0].getAttribute("data-events");

    fetch("testedropdown.json")
        .then(response => response.json())
        .then(data => {
            console.log(data.Stages[selectedstages].Events[selectedevents].Eps);
        })
        .catch((err) => {
            console.log(err);
            // HANDLE ERRORS HERE.
        });
}

More info here.

Ernesto Stifano
  • 3,027
  • 1
  • 10
  • 20