In an HTML document, a script is run when the page loaded and the JS defines a series of chained together functions, all called whenever the previous one is done. I have a number of questions with this example:
- Why is
f1undefined? - Am I supposed to be able to define functions
f1, ...,f4also withincallbacksInit, so as not to pollute the global namespace? - Am I supposed to be able to define functions
f1, ...,f4in the JS document aftercallbacksInit?
var f1 = function() { console.log("Running f1"); }
var f2 = function() { console.log("Running f2"); }
var f3 = function() { console.log("Running f3"); }
var f4 = function() { console.log("Running f4"); }
function callbacksInit() {
function start() {
f1()
.done(f2)
.done(f3)
.done(f4);
}
start();
}
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="./callbacks.js"></script>
</head>
<body onload="callbacksInit()">
<main>
<h1>CALLBACKS ASYNC</h1>
</main>
</body>
</html>
f1()returnsundefinedand you expect it to be a Promiseasync function, in which case they will implicitly return a Promise. However, since you don't have any async functionality, I don't know why you want to chain.donehere.async/awaitfor those functions which is basically equivalent but you may prefer to just useawaiteverywhere.