Email or username:

Password:

Forgot your password?
Top-level
diabhoil

@mariusor have something like a global dictionary (object) of type <string, Promise>. If the url is already a key in the dictionary then just resolve the promise again, otherwise add to dictionary and resolve.

5 comments
marius

@diabhoil yes, that's the way I was leaning towards also, but I wasn't sure it was very idiomatic. Do you have an example about how you use that ?

diabhoil

@mariusor No not really. But you could have a wrapper function like "cachedFetch(url) { if(!dictionary[url]) dictionary[url] = fetch(url); return dictionary[url] }"

dont know if the syntax is correct :D And its not tested...but something like that was in my mind ^^

marius

@diabhoil yes, that's exactly what I started with, however the fetches happen all before the promises resolve, so the check fails for the URL. Maybe I just need to stick the promises in there.

diabhoil

@mariusor just tested with this const cachedFetch = (url) => { if(!dictionary[url]) { console.log("add to cache"); dictionary[url] = fetch(url); } else { console.log("just use from cache!"); } return dictionary[url]; } and it worked like expected when calling "await cachedFetch(url)"

marius

@diabhoil then maybe I get tripped by some awaits I have in the code. Thank you.

Go Up