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

153
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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