Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Last active January 25, 2020 01:09
Show Gist options
  • Save lovasoa/d55631dae939c4ed6b449c102a9ac6f6 to your computer and use it in GitHub Desktop.
Save lovasoa/d55631dae939c4ed6b449c102a9ac6f6 to your computer and use it in GitHub Desktop.
ES6 async memoize, using async/await and a Map to store the cache.
function memoize_async(func) {
const m = async function (key) {
if (!m.cache.has(key)) m.cache.set(key, await func.apply(this, arguments));
return m.cache.get(key);
};
m.cache = new Map();
return m;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment