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

125
Views
Why does the browser not freeze when awaiting these promises?

I am having a hard time understanding why the browser does and does not freeze with awaiting the promises specified in the below HTML code:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Promise Test</title>
        <style>
            #myBlock{
                height: 400px;
                width: 400px;
                background-color: black;
            }

            #myBlock:hover{
                background-color: red;
            }
        </style>
   </head>
    <body>
        <div id="myBlock"></div>
        <br>
        <button onclick="awaitPromise()">Await a promise</button>
        <br>
        <h3 id="status">Promise Status:</h3>

        <script>
            async function awaitPromise(){
                document.getElementById("status").innerHTML = "Promise Status: Awaiting Promise";
                
                await new Promise(resolve => setTimeout(resolve, 5000));
                
                /*
                await new Promise(()=>{

                });

                await new Promise(()=>{
                    while(true){

                    }
                });
                */

                document.getElementById("status").innerHTML = "Promise Status: Finished";
            }
        </script>
    </body>
</html>

For the 1st promise:
I expected the browser to be frozen for 5 seconds since the promise would not be resolved for 5 seconds, but the browser never freezes.

For the 2nd and 3rd Promise:
I expected the browser to freeze since the function would be halted permanently. However, only the 3rd promise freezes the browser.

It appears to me that as long as the function given to the promise finishes, the gui will not freeze regardless of whether or not the promise is resolved.

Can someone explain to me why the browser does not freeze even when the awaitPromise function is not finished running? And also why does the browser freeze only when awaiting the 3rd promise? Thanks.

about 4 years ago · Juan Pablo Isaza
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!