0

I was wondering what was going on in the following code block:

    async function test() {
        function work() {
          console.log("work");
        }

        await setTimeout(work, 5000);
        await setTimeout(work, 1000);
        await setTimeout(work, 5000);
        await setTimeout(work, 1000);
        await console.log("Hello")
      }

This code block takes 5 seconds total to run and 'Hello' is logged immediately, implying all the functions are executing at the same time. How would I make these functions run sequentially without using Promise.all()? Why is this behavior occuring?

  • 3
    It's wrong. `setTimeout()` doesn't return a promise, you can't await it. – Barmar Apr 05 '22 at 20:36
  • Isn't this a duplicate of the question you asked just over a half hour ago? [Do await functions have to resolve to Promises for them to run sequentailly?](https://stackoverflow.com/questions/71757734/do-await-functions-have-to-resolve-to-promises-for-them-to-run-sequentailly) – esqew Apr 05 '22 at 20:37

0 Answers0