Are you using map, forEach, and reduce wrong?
In JavaScript, Array.prototype.map, Array.prototype.forEach, and Array.prototype.reduce are used heavily in functional-style programming. However, I meet many developers missing a clear mental model of when or how to use each function.
This is a prob...
antman-does-software.com12 min read
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.