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

166
Vistas
Do i need to make this function asynchronous to wait for the filter method?

I have the state array "days" and I have a function that gets the element with an id given by the argumet, i am using the array.filter method to do this. Assuming the array has a lot of data in it, do i need to make the function asynchronous to wait for the filter method to print that data?

const [days, setDays] = useState([]); 
const onDayClicked = async (id) => {
    const daySelected = await days.filter(el => el._id === id)
    console.log(daySelected)
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

filter returns:

A new array with the elements that pass the test. If no elements pass the test, an empty array will be returned.

An array is not a promise.

await is only useful if you have a promise on the RHS.

If you have an expensive, blocking operation to do, then it is simply an expensive operation that will block the event loop. You can't get around that by throwing promise syntax at it.

You could get around it by moving the work out of the main event loop and into a web worker.

If you did that, then you could create a promise that resolves when the web worker sends the results back to the main event loop.

And if you did that, then you could use await so that daySelected would have the resolved value in it.

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