In the following JS code, I fetch some data but I really need to stop the code after the catch if there is one and I can’t find my way to do it.
Could anyone explain to me why it’s not stopping (or why I can’t do it like this)?
const parameters = {
idCampaign: idCampaign ?? null,
};
const body = new URLSearchParams(parameters).toString();
try {
fetch('function/loadIdQuestionsForm.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: body
}).then((response) => {
if (!response.ok) {
return response.json();
}
throw new Error("Failed to get data");
}).then((responseData) => {
console.log(responseData);
}).catch((error) => {
throw error;
});
}catch (error){
notifBar(error.message, 0);
// stop here if there's an error
}
As you can see I tried to wrap my fetch with another try catch but same result, the script continues to run.