Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

376
Visualizações
Fetch call results in net::ERR_CONNECTION_REFUSED, how to fetch a another server when error arises

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.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda