Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda