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

155
Vistas
Filtering a list of promises inside useEffect

I found that it's not straight forward to filter a list of values of promises, so I tried the approach in this question (top answer).

The difference is, i'm using this inside useEffect in React.

My results array is empty outside of the async getData function:

// helper function
async function filter(arr, callback) {
  const fail = Symbol();
  return (
    await Promise.all(arr.map(async (item) => ((await callback(item)) ? item : fail)))
  ).filter((i) => i !== fail);
}

useEffect(() => {
      if (!stateData) {
        let results = [];

        async function getData() {
          const res = await filter(data.slice(0, 5), async (item) => {
            const boolRes = await myAsyncFunction(item);
            return boolRes;
          });
          // results has data 
          results.push(...res);
        }
        getData();
        console.log(results) // is empty, but results inside getData has data
      }
  }, []);

any idea how I can pull the data out of getData safely?

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