I am developing an app in javascript that requires me to make fetch calls to a secondary server when the primary is down. I have shutdown the primary in order to have the app fetch the secondary server. The fetch call for my first effort looks something like this.
try {
fetch(`https://server1`)
.then(response => {
if (!response.ok) {
fetch(`https://server2`)
.then(response => {
return response.text();
})
}
return response.text();
})
} catch (err) {
}
As shutting down the server results in net::ERR_CONNECTION_REFUSED I have nothing happens in the catch error. I have also tried adding a flag variable and having it run through fetching the first server then the second as a result of the flag value:
var flag = false;
try {
fetch(`https://server1`)
.then(response => {
if (response.ok) {
flag = true;
}
return response.text();
})
.catch((err) => {
flag = true;
});
if (!flag) {
fetch(`https://server2`)
.then(response => {
if (response.ok) {
flag = false;
}
return response.text();
});
}
}
Any tips appreciated. Thank you.