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

157
Vistas
UncaughtAssertionError:expected promise to be rejected with error including 'Falsy value' but was fulfilled with {Object(_idleTimeout,_idlePrev, ...)}

Writing a function which creates and returns a promise.

Run a given (callback) function after a delay. However:

  • if the given callback returns a falsy value, the promise should fail (reject) the string "Falsy value retrieved" should be sent through to the reject function
  • if the given callback returns a truthy value, the promise should pass (resolve) the return value of the executed callback should be sent through to the resolve function
const doShortlyExpectingTruthy = function(callback, delay, data) {
  const promise =  new Promise((resolve, reject) => {
    let returnValue = setTimeout(callback, delay, data);
    if (returnValue) {
      resolve(returnValue);
    } else if (!returnValue) {
      reject("Falsy value");
    }
  });
  return promise;
};

May I know how to fix this Uncaught Assertion Error.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

setTimeout's return value is an integer id used to cancel the timeout, not the return value of the callback. If setTimeout worked synchronously like this, there'd be no need for the promise.

I'd break out a generic sleep function to keep the new Promise constructor out of your code, then use async/await. throw in an async function is the same as calling reject.

const sleep = ms => 
  new Promise(res => setTimeout(res, ms))
;

const doShortlyExpectingTruthy = async (fn, delay, ...args) => {
  await sleep(delay);
  const res = fn(...args);
  
  if (!res) {
    throw Error("Falsy value retrieved");
  }
  
  return res;
};

(async () => {
  console.log(await doShortlyExpectingTruthy(v => v, 1000, 42));
  await doShortlyExpectingTruthy(v => v, 1000, 0);
})()
  .catch(err => console.error(err))
;

about 4 years ago · Juan Pablo Isaza Denunciar
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