I'm implementing loading of files (JSON files in this case) using fetch. I found this solution to catch any error (in my case, the error would be that there is no such file, which is a normal possible condition).
The piece of code I'm using is:
fetch(l_Real_Reference).then(
response => {
if (response.ok) {
return response.json(); }
throw new Error('Could not load file (there is no file)');
})
.catch((error) => {
console.log("Catched error: " + error)
return ;
})
.then(data => {
if (p_What == "Master_Index") {
G_All_Albums = JSON.parse(JSON.stringify(data)) ;
Populate_Albums_List() ;
}
When attempting to load an file that does not exist (as yet!), the error shown in the console is:
As can be seen, the catch is activated but the flow of the code is still terminated.
What needs to be changed in the presented code such that the normal flow of the code is not disrupted?