Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Testing a memoize async function

Need some help in writing test cases. I want to test whether getSomeData is called only once.

function getSomeData(foo, callback) {
  return new Promise((resolve, reject) => {
      setTimeout(()=> {
          console.log('async request');
          resolve(callback(2 * foo));
      }, 1000);
  });
}

Now I want to test whether memoized function is called once or twice even if memoizedGetSomeData is called twice

function memoize(fn) {
  const cache = {};
  return async function() {
    const args = JSON.stringify(arguments);

    console.log('arguments passed to memoize fn: ', args);
    console.log('cache data: ', cache);

    cache[args] = cache[args] || fn.apply(undefined, arguments);
    return cache[args]
  }
}

const memoizedGetSomeData = memoize(getSomeData);

memoizedGetSomeData(1, callback_fn);
memoizedGetSomeData(1, callback_fn);
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!