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

211
Views
Fetch, then and setTimeout

How can I do setTimeout in then block? I set setTimeout there, but program just goes through and does not wait for anything.


 const controller = new AbortController();
  const { signal } = controller.signal;
  return fetch(url, { signal })
    .then((response) => {
      if (response.status === 404) {
        controller.abort();
        setTimeout(() => controller.abort(), 5000);
      }
      return response;
    })
    .then((response) => {
      console.log('in');
      setTimeout(() => controller.abort(), 5000);
      return response.text();
    })
    .catch(() => {
      return Promise.reject(new Error('empty link'));
    });

I tried to set settimeout in then block, in catch block, tried to use for it the function which would return promise. The logic seems simple, but somehow I don't understand something about how to connect then block and settimeout functionality.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The setTimeout() call doesn't block. If you want to "await" for x milliseconds, you can do the following:

const timeout = (ms) => {
    return new Promise(resolve => setTimeout(resolve, ms));
}
const doSomething = async () => {
    await timeout(3000);
    // after 3 seconds
}
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!