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

124
Visualizações
Resolve n from promise and create n promises - how do I do this without nesting promises?

I'm working in node and must make n amount of API calls after getting n returned from a promise. My initial thought would be to create a foreach loop that creates n promises, store them in an array, and then call

Promise.all(promisesArray).then(function(){...});

However, this would have the be wrapped in the then() call from the original promise, effectively setting up a nested promise situation.

getN() //returns a promise object.
.then(function(n) {
    var promisesArray = [];
    for(var i = 0, i < n; i++) {
        var promise = new Promise(function(resolve, reject) { callSomething(i, resolve) });
        promisesArray.push(promise);

    }
    Promises.all(promisesArray).then(function(){
        ... 
        resolve; //resolves original promise
    });
.then(function(something) {...})
.catch(function(err) {...};

How can I avoid this? Thanks for your help!

over 4 years ago · Santiago Trujillo
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