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

182
Visualizações
multiple request responses with promise.all() and map javascript

I'm trying to get multiple responses from the server. For that I need all the requests to be sended (I don't mind the order) and to get all the responses before returning them. This is my code:

async function getResponse(URL){
    var xhttp;
    xhttp=new XMLHttpRequest();
    xhttp.responseType = "json"
    xhttp.open("GET", URL, true);
    xhttp.send();
    xhttp.onreadystatechange = async function() {
    if (this.readyState == 4 && this.status == 200) {
        response = await this.response
        console.log("response")
        console.log(response)
        return response
    }
    };
}

async function getFilteredDiagram(){
    let requests = ['/?s=1&a=2','/?s=3']
    var responses = await Promise.all(requests.map(async (r) => {return await getResponse(r)}))
    console.log("all_responses")
    console.log(responses)
}

The thing is that I'm doing console.logs to see whats happening and I get:

all_responses: 
[undefined,undefined]
response:
[resp1]
response:
[resp1,resp2]

That is to say, it is applying the mapping over the list, before getting a response from getResponse. I need them to be in the other way.

Can someone help me?

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