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

108
Visualizações
send multiple ajax post requests at regular intervals until any one of them passed

I am writing a test program for website most of time the website gives 504 error. i want to write a program that can sent ajax requests to server and replace page with server response html. the process of requests is like :

  1. send 20 requests at a time if any of them got 200 response then cancell all other requests
  2. repeat step 1 after 10 seconds until any request got valid response.

currently i am making 20 ajax calls and the wait for any of them to complete using Promise.any(). the problem i am facing is that how can i manage all batches in one Promise.any() because i need to keep track of previous batch as well like if one of second batch calls completed i need to cancell all first batch calls and remaining 2nd batch calls. Hope you got my point what i want. here is the code i have done so far

var reqs = [];
async function post_page(url, i){

    var formElement = document.getElementById('form1');
    var data = new URLSearchParams(new FormData(formElement));
    data.append('continue', 'Continue');

    let controller = new AbortController();
    var req = fetch(url, {
        method: 'post',
        body: data,
    }
     ).then(response => {
      if (!response.ok) {
            return Promise.reject('not ok');
        }
      return (response, i);
  });
    reqs.push(controller);
};

var load_page = function(url){
    var promises = [];

    for(var i = 0 ; i < 10; i++){
        promises.push(post_page(url, i));
    };

     Promise.any(promises).then((value, index) => {
         reqs.splice(index,1);
         $.each(reqs, function(id, req){req.abort();})
        loadPageHtml(value);
    })
};
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