Skip to content Skip to sidebar Skip to footer

For Vs ForEach Issue

if for and forEach are both synchronous why does the forEach loop not work and always return false in this case? I keep thinkining it's too early in the morning and I have made a g

Solution 1:

The return inside the callback you pass to .forEach() just terminates that function call, not the "findItem2" call. In other words, the .forEach() mechanism itself has called the function you've passed it, and your return statement just returns from that; the overall process continues. Nothing ever pays attention to the value you return.

This is a case where the plain for loop is probably a little clearer. Some function frameworks might offer something like .forEach() specialized for the task of locating the first element in a list that satisfies a given condition.


Post a Comment for "For Vs ForEach Issue"