'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)
}


Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1