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

260
Vistas
How to supply metadata about the promises when calling Promise.all()?

I'm working on some Javascript code that looks like this:

const arrayOfPromises = getArrayOfPromises();
Promise.all(arrayOfPromises).then(array => doStuff(array));

I need to change getArrayOfPromises() to return some information about each promise - for simplicity, let's say it's a simple true/false flag for each. My first instinct was to refactor this function to return an array of objects instead, e.g:

const getArrayOfObjects = () => {
    ...
    return [
             { promise: promise1, flag: flag1 },
             { promise: promise2, flag: flag2 },
             ...
             { promise: promisen, flag: flagn }
           ];
}

However, the above wouldn't work with Promise.all because it only works directly with an iterable of Promise objects without the wrapper objects in between.

What's the simplest way to supply such metadata for each of the promises and then work with it once all the promises have been resolved?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you want to use this meta data once the promises have been resolved and you don't have any control over the values that are being resolved by the promises returned from getArrayOfPromises(), then you can simply call then() on every separate promise in the array and make it return the original value combined with your meta data. Though make sure you replace the original promise with the promise returned by the .then(). Array.prototype.map() makes this easy:

arrayOfPromises = arrayOfPromises.map(promise => promise.then(value => ({
  value,
  flag: true
})));

Example:

let arrayOfPromises = getArrayOfPromises();
arrayOfPromises = arrayOfPromises.map(promise => promise.then(value => ({
  value,
  flag: true
})));

Promise.all(arrayOfPromises).then(array => doStuff(array));

function getArrayOfPromises() {
  return [sleep(1000), sleep(1500), sleep(2000)];
}

function doStuff(array) {
  console.log(array);
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(() => resolve('resolved after ' + ms), ms));
}

If you do have control over what getArrayOfPromises() returns, then simply return the meta data as part of the value that is being resolved.

about 4 years ago · Juan Pablo Isaza Denunciar

0

What's the simplest way to supply such metadata for each of the promises and then work with it once all the promises have been resolved?

Just put the data in each result value. It's not metadata about the promise, but about the value (?), and you don't need to work with it until all promises are resolve, so the results are the perfect place. The flags just become contents of the array that Promise.all(…) resolves to.

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