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

244
Visualizações
HTTP fetch info IN ORDER and add to array in the order specified

I have tried it all. I want to fetch from my server some data in a loop, and add the ressponses in an array in the order I run the for loop.

I am thinking the answer is in recursion, but tried it several times and failed.

I am using AXIOS:

let currentPhraseWordAssign = [];

function addPartOfSpeechAndWordPhrasesToFullScript () {
    assignPartOfSpeechAndAddToPhraseArray(["phrase", "run"]).then(() => {console.log(currentPhraseWordAssign)});
}


async function assignPartOfSpeechAndAddToPhraseArray (phrase) {
    if(currentPhraseWordAssign.length >= phrase.length) {
        console.log(currentPhraseWordAssign);
        currentPhraseWordAssign = [];
        return;
    }
    let word = phrase[currentPhraseWordAssign.length];
    axios({
        method: 'get',
        url: `http://localhost:9200/english_minus_verbs/_search`,
        headers: {
            'Content-Type': 'application/json'
        },
        data: {
            _source: ["part_of_speech"],
            query: {
                term: {
                    word: word,
                }
            }
        }
    }).then((r) => {
        try {
            currentPhraseWordAssign.push({word: word, partOfSpeech: r.data.hits.hits[0]._source.part_of_speech});
        }catch (e) {
            currentPhraseWordAssign.push({word: word, partOfSpeech: 'verb'});
        }
    }).then(()=>{
        assignPartOfSpeechAndAddToPhraseArray(phrase);
    });
}

Maybe that code is completely wrong, it is anyway like the 7th time I write it... I tried with promises, async/await, sync, and now recursion...

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can run the axios calls in sequence (one after the other) like this:

async function lookupWords(arrayOfWords) {
    let currentPhraseWordAssign = [];
    for (let word of arrayOfWords) {
        const data = await axios({
            method: 'get',
            url: `http://localhost:9200/english_minus_verbs/_search`,
            headers: {
                'Content-Type': 'application/json'
            },
            data: {
                _source: ["part_of_speech"],
                query: {
                    term: {
                        word: word,
                    }
                }
            }
        });
        currentPhraseWordAssign.push({word: word, partOfSpeech: r.data.hits.hits[0]._source.part_of_speech});        
    }
    return currentPhraseWordAssign;
}

Or, you can run them in parallel and collect the results in order:

function lookupWords(arrayOfWords) {
    return Promise.all(arrayOfWords.map(word => {
        return axios({
            method: 'get',
            url: `http://localhost:9200/english_minus_verbs/_search`,
            headers: {
                'Content-Type': 'application/json'
            },
            data: {
                _source: ["part_of_speech"],
                query: {
                    term: {
                        word: word,
                    }
                }
            }
        }).then(r => {
            return {word: word, partOfSpeech: r.data.hits.hits[0]._source.part_of_speech};
        });
    }));
}

If you run them in parallel (which may finish sooner), you have to make sure that the target server will accept however many simultaneous requests as your array is long. Some servers will limit or reject too many simultaneous requests. To run semi-in-parallel where you have N requests in flight at the same time (how you would process a large array in parallel without too many requests in flight at once), you would use something like mapConcurrent() to control the execution.

about 4 years ago · Juan Pablo Isaza Relatório
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