Promises & then/catch
What optimization pattern does this implement?
const cache = new Map();
function memoize(fn) {
return function(...args) {
const key = JSON.stringify(args);
if (cache.has(key)) {
return Promise.resolve(cache.get(key));
}
return fn(...args).then(result => {
cache.set(key, result);
return result;
});
};
}