Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

111
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda