Home > Software engineering >  prompt loads before anything else
prompt loads before anything else

Time:02-11

hay if anybody understand why is prompt showing up even before the console printing
i appreciate the help

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>promptheadache</title>
</head>

<body>
    <script>
    
        console.log('i should appear first on the console')

        var promptInput = prompt('I\'m am here befor anything else')

    </script>
</body>

</html>

CodePudding user response:

console.logs are actually asynchronous in that they are synchronized (as in always in order) but perform an asynchronous call.

prompt is a very old API and one of the few that actually blocks the page - so even-though it appears after the console.log you see it as soon as it is called.

That said - this is not true for every console nor is it guaranteed - the console.log may appear before the prompt depending on the browser/console implementation.

CodePudding user response:

If you want to get the console before prompt you can add setTimeOut-setTimeout() is an asynchronous function.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>promptheadache</title>
</head>

<body>
    <script>
    
        console.log('i should appear first on the console')
  setTimeout(() => {var promptInput = prompt('I\'m am here befor anything else')
},5000) 
 
       

    </script>
</body>

</html>

  • Related