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

152
Views
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 answers
Answer question

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 Report
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!