Last active
January 25, 2020 01:09
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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