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

171
Visualizações
How to use axios.all to 2d array in Node.js?

I want to use an api to know status of 3*2 urls, and I wrote this source code but it didn't work. Maybe because that newList function returns 2d array functions so. And in this code, newList function works twice like, I don't know why exactly.

Could you please tell me how to simplify and make work this code normally?

axios api

const axiosInstance = axios.create({ timeout:3000 });
let server = new Array(3)
server[0] = 'http://serverUrl0:'
server[1] = 'http://serverUrl1:'
server[2] = 'http://serverUrl2:'
let postfixA = '12345/?version=0'
let postfixB = '12222/?version=1'
let serverList = createArray(3,2) // make array[3,2]
for (let i =0;i<server.length; i++){
    for(let j=0; j<2 ; j++){
        serverList[i][0] = {url : server[i]+postfixA, result : 200}
        serverList[i][1] = {url : server[i]+postfixB, result : 200}
    }
}

function newList(){
    return serverList.map((valueArray, i )=>{
        return valueArray.map((value, j)=>{
            return axiosInstance.get(
                value.url
            ).catch(function (error){
                console.log(`lnbServers server ${i+1} ${j==0 ? 'A':'B'} Error >>`, error.message)
                value.result = 500
            })
        })
    })
}

try {
    await axios.all(newList())
    
    
    let data = {}
    for(let i=0; i<server.length; i++){
        let keyStr = 'server_'+i
        for(let j=0; j<2; j++){
            keyStr+= j%2==0 ? 'A' : 'B'
            data[keyStr] = serverList[i][j].result
        }
    }
    response.json(data)
    
} catch (err) {
    console.log(err)
    response.sendStatus(500)
}
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