DEV Community

Discussion on: Async/Await and the forEach Pit of Despair

Collapse
 
iurquiza profile image
iurquiza • Edited

'forEach' and 'map' are declarative functions used to traverse/manipulate all items in an array. You are not supposed to assume how they work internally, including how they access the items. In many cases/languages they are use to parallelize execution, which basically throw order of completion out of the window. If your task depends on the order of completion use a procedural loop. forEach's internal implementation can use a typical loop but it could also be implemented using a different traversal method.