Guest User

Untitled

a guest
Jun 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. /* how it works:
  2. const getName = name => console.log(`Your name ${name}`);
  3. const yourName = cachedFunction(getName, 10000);
  4. yourName('viktor') :: -> Your name viktor
  5. yourName('viktor') :: -> ...
  6. ... // after 10 sec
  7. yourName('viktor') :: -> Your name viktor
  8. */
  9.  
  10. import {
  11. memoize,
  12. throttle,
  13. } from 'lodash';
  14.  
  15. // cachedFunction :: function, number -> function
  16. const cachedFunction = (func, duration) => {
  17. const memoizedFunction = memoize((key) => {
  18. const throttledFn = throttle(func, duration, { trailing: false });
  19.  
  20. return () => throttledFn(key);
  21. });
  22.  
  23. return (...args) => memoizedFunction(...args)();
  24. };
  25.  
  26.  
  27. export default cachedFunction;
Add Comment
Please, Sign In to add comment