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

243
Visualizações
Is there a way to make main code wait for a nested fetch?

I am working on a Slack bot that fetches merge requests for given list of repositories (so multiple).

const fetchRepositories = (repositories) => {
    return repositories.reduce(async (accumulator, currentRepository) => {
        const eligibleMRs = await fetchEligibleMergeRequests(currentRepository.api);
        // the PROBLEM is here
        const refinedMRs = eligibleMRs.length && eligibleMRs.map(mr => {
            // some logic to clean the shorten info received
        }

        return Promise.resolve(accumulator)
            .then(...) //return personalized object for each repository
    }, []);
};

The problem is that eligibleMRs is an array of Promise { <pending> } (each merge request).

And this problem appeared when instead of only fetching all repository merge requests, I added another .then that fetches the list of reviewers for every mr.

const fetchEligibleMergeRequests = (url) => {
    return fetch(url, fetchOptions)
        .then(response => {
            return response.json();
        })
        .then(data => {
            return data.filter(mr => filterMergeRequest(mr));
            // when I only had this .then everything was perfect
        })
        .then(async filteredMRs => {
            return filteredMRs.map(async mr => {
                const fetchedReviewers = await fetchReviewers(mr["project_id"], mr.iid);
                //this fetch is another call to GitlabAPI in order to retrieve reviewers
                return Object.assign({}, mr, {
                    reviewers: fetchedReviewers
                });
            });
        })
};

What I would like to ask help for is the fact that the newly added fetch (the one that gets the reviewers for each mr) seems to hold everything back. And I would like to make the main code TO WAIT for reviewers fetching.

Thanks to all trying to help.

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