Praktikum_mf

Мемоизация №1

Mar 30th, 2021 (edited)
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function memoize(fn, timeout) {
  2.     let cache = null;
  3.     let expire = Date.now() + timeout;
  4.  
  5.     return function() {
  6.         if (!cache || Date.now() > expire) {
  7.             return fn().then(data => {
  8.                 cache = data;
  9.                 expire = Date.now() + timeout;
  10.                 return data;
  11.             });
  12.         }
  13.  
  14.         return Promise.resolve(cache);
  15.     }
  16. }
Add Comment
Please, Sign In to add comment