Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!