0

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.

bugthefifth
  • 161
  • 15
  • 1
    What is your ultimate goal? To pause all execution for 5 seconds? Why? – jarmod Jul 12 '22 at 16:24
  • 2
    Whats the point of using `controller.abort` when the fetch has finished? (after "then" has been executed)? What are you trying to do? – Pipe Jul 12 '22 at 16:25
  • "*program just goes through and does not wait for anything.*" - yes, nothing in your could waits for `setTimeout`. Why would it? – Bergi Jul 12 '22 at 16:28
  • The goal is to try the url, if it's empty, wait for 5 seconds. While this 5 seconds program does something else, after 5 seconds, I go to different url. – bugthefifth Jul 12 '22 at 16:28
  • `setTimeout` is not a promise, but you can make it into one -> `const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));` You can then do `sleep(5000).then(...` – Keith Jul 12 '22 at 16:28
  • @bugthefifth "*program does something else, after 5 seconds, I go to different url*" - where's the code for that? Please post your complete program. And still this doesn't answer why you're using an `AbortController`. – Bergi Jul 12 '22 at 16:32
  • Does this answer your question? [What is the JavaScript version of sleep()?](https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep) – Samathingamajig Jul 12 '22 at 16:35
  • abortController to stop connecting to url. controller.abort(). I tried to keep the question simple. I really cannot connect then and settimeout. Somehow I have wrong idea about it. – bugthefifth Jul 12 '22 at 16:37

1 Answers1

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
}
lukasl-dev
  • 724
  • 3
  • 11
  • Ty for answer. I'll be going home next 1.5 hour. I'll try the answer and advises at home. Our community is amazing. Thank you. – bugthefifth Jul 12 '22 at 16:42
  • No it didn't work for me. I don't have async/await blocks. I have then chain. But anyway I set it into the chain, and it just doesn't wait, it goes through all links and strangely enough goes, doesn't run promise at all. I tried to set the closure if response.state === 404, but it didn't help either. – bugthefifth Jul 13 '22 at 08:27
  • Did you use the timeout() call within an async function callback? – lukasl-dev Jul 13 '22 at 14:48