Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda