Drunk on types and functional programming
Former CTO of SKUTOPIA I write about Functional Programming, TypeScript, Node, Event Sourcing, and engineering leadership
Markdown supported · ⌘↵ to post

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!
Benjamin J
Interesting article.
A friendly heads up that I think there's a subtle race condition in your
getArticleTypeById. It loads in everything fromarticle_types, but it is invoked bymap-- so it could be called many times in close succession.The problem is that the
cachelocal variable is set only after queryingarticle_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 aMap<...>. 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 theCURSOR_REPO_MAPand updating theCURSOR_REPO_MAP, so not 100% guaranteeing a singleton.