DEV Community

Discussion on: Washing your code: avoid loops

Collapse
 
itsjzt profile image
Saurabh Sharma • Edited

while I don't find for of loops less readable than any functional array transformation.

I use functional methods heavily for array transformations or when I use React.

Other than that I find loops more readable when logic is longer than 3, 4 line or contains many if/else.

In async stuff loops are much better because in map/reduce you need to await the map/reduce and make the inner function async and also await inside it.

for ex:

async function getUsers (userIds) {
  const users = userIds.map(async userId => {
    const response = await fetch(`/api/users/${userId}`);
    return response.json();
  });
  // ... do some stuff
  return users;
}