~reggie/blog

dd56f81b6b37377b1f0a6574936fd46717548aa7 — Reggie 3 months ago f7cf986
feat: add socials to blogpost
1 files changed, 13 insertions(+), 2 deletions(-)

M content/blog/caching-promises-in-javascript.md
M content/blog/caching-promises-in-javascript.md => content/blog/caching-promises-in-javascript.md +13 -2
@@ 5,6 5,17 @@ draft: false
syntax: true
description: >
    Caching multiple concurrent calls to an async function in JavaScript.
socials:
    [
        {
            name: Hacker News,
            link: "https://news.ycombinator.com/item?id=37266951",
        },
        {
            name: Mastodon,
            link: https://defcon.social/@reggie/110952278669061422,
        },
    ]
---

# Reasoning


@@ 39,7 50,7 @@ All of them would check for `"mykey"` and get a response. Since this is all
before the first call was done executing, all the responses will be empty and
each of our ten calls will generate new content and store it in the database.
Redis is pretty fast, but we still don't want to do 20 calls where 2 would
suffice, or even worse, 10 potentially expensive function calls. 
suffice, or even worse, 10 potentially expensive function calls.

Redis is pretty fast, and it can surely handle setting the same key several
times, but I feel like that's what you call a race condition.


@@ 128,7 139,7 @@ export function promiseStoreFn(fn) {

    return async (param) => {
        return await store.resolve(param, async () => await fn(param));
    }
    };
}
```