mercredi 10 juillet 2019

JavaScript callback running out of order in Chrome?

I am trying to call a function AFTER another function has completed. Normally, this is done with callbacks, at least with Node.js. However, when I try to run the following code in Chrome, the callback function seems to execute before the main function. Am I writing my function/callback wrong? Shouldn't the second function (the callback function) only execute after the first one is complete?

If callbacks don't work when the javascript is running client-side in the browser, is there another way I can ensure the second function runs only when the first function is complete?

<html>
<head></head>
<body>
<script>

function firstLoad(callback) {
  console.log("firstLoad function fired.");
}

function secondLoad() {
  console.log("secondLoad function fired.");
}

firstLoad(secondLoad());

</script>
</body>
</html>

In Chrome Developer Tools Console, the above code gives me:

secondLoad function fired.

firstLoad function fired.

I would expect it to be the other way around.




Aucun commentaire:

Enregistrer un commentaire