---
title: "Discussion on \"Are you using map, forEach, and reduce wrong?\" | Hashnode"
description: "Discussion on \"Are you using map, forEach, and reduce wrong?\". Make your code readable with this definitive guide to best practices for using each function."
url: "https://hashnode.com/posts/are-you-using-map-foreach-and-reduce-wrong/6236f5328e4fae3f767de87c"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Are you using map, forEach, and reduce wrong?](https://antman-does-software.com/are-you-using-map-foreach-and-reduce-wrong?utm_source=hashnode&utm_medium=feed)

Make your code readable with this definitive guide to best practices for using each function.

Mar 20, 2022 · 12 min read · antman-does-software.com  
Updated Mar 27, 2022  
Read the article

-   [#typescript](https://hashnode.com/tag/typescript)
-   [#javascript](https://hashnode.com/tag/javascript)
-   [#functional-programming](https://hashnode.com/tag/functional-programming)

## Written by

[Anthony Manning-Franklin](https://hashnode.com/@Antman261)

@Antman261 · Perth, Australia

  
[View profile](https://hashnode.com/@Antman261)

Drunk on types and functional programming

Former CTO of SKUTOPIA I write about Functional Programming, TypeScript, Node, Event Sourcing, and engineering leadership

## Responses 2

Markdown supported · ⌘↵ to post

[Benjamin J](https://hashnode.com/@benj123)

[Oct 25, 2023](https://hashnode.com/posts/are-you-using-map-foreach-and-reduce-wrong/6236f5328e4fae3f767de87c/comment/6538bef0c6b0d16a577016f5)

Interesting article.

A friendly heads up that I think there's a subtle race condition in your `getArticleTypeById`. It loads in everything from `article_types`, but it is invoked by `map` -- so it could be called many times in close succession.

The problem is that the `cache` local variable is set only after querying `article_types`, so this means that it's possible to have multiple Postgres queries outstanding. I suppose you're using a pool, so the impact will be limited by the pool size. However, it would mean that the entire pool could be temporarily exhausted before the cache is built and set.

Perhaps one way to fix it would be to make cache a `Promise<Map<...>>` instead of just a `Map<...>`. Something like this:

```
const buildCache = async () => {....};
if (!cache) {
  // note: no await here - we want the promise
  cache = buildCache(); 
}
return (await cache).get(id);
```

There's something similar in your Outbox pattern blog post too - awaiting on `connect()` between checking the `CURSOR_REPO_MAP` and updating the `CURSOR_REPO_MAP`, so not 100% guaranteeing a singleton.

[Mohit Yadav](https://hashnode.com/@just-moh-it)

[Mar 20, 2022](https://hashnode.com/posts/are-you-using-map-foreach-and-reduce-wrong/6236f5328e4fae3f767de87c/comment/62378fae8e4fae3f767dff97)

![Great](https://media.giphy.com/media/28exZ74FVa4gHd660z/giphy.gif)

The explanation is great! I'm still a newbie to the reduce function, so I try to avoid it mostly, but at least I got the `map` and `forEach` functions correct!
