Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* how it works:
- const getName = name => console.log(`Your name ${name}`);
- const yourName = cachedFunction(getName, 10000);
- yourName('viktor') :: -> Your name viktor
- yourName('viktor') :: -> ...
- ... // after 10 sec
- yourName('viktor') :: -> Your name viktor
- */
- import {
- memoize,
- throttle,
- } from 'lodash';
- // cachedFunction :: function, number -> function
- const cachedFunction = (func, duration) => {
- const memoizedFunction = memoize((key) => {
- const throttledFn = throttle(func, duration, { trailing: false });
- return () => throttledFn(key);
- });
- return (...args) => memoizedFunction(...args)();
- };
- export default cachedFunction;
Add Comment
Please, Sign In to add comment