I have easy noob question concerning control flow of basic app. I have 3 functions with setTimeout inside.
console.log("[+] Program start");
function first(){
setTimeout(function(){
console.log(1);
},3000);}
function second(){
setTimeout(function(){
console.log(2);
},2000);}
function third(){
setTimeout(function(){
console.log(3);
},1000);}
first();
second();
third();
console.log("done");
Output is as expected this:
[+] Program start
done
3
2
1
I would like to control flow that I will see things in following order:
[+] Program start
1
2
3
done
So I rewrote program by following way:
console.log("[+] Program start");
function first(){
setTimeout(function(){
console.log(1);
second();
},3000);}
function second(){
setTimeout(function(){
console.log(2);
third();
},2000);}
function third(){
setTimeout(function(){
console.log(3);
call();
},1000);}
first();
function call(){console.log("done ");}
Output is:
[+] Program start
1
2
3
done
Now output is ok, I would like to ask you, is this approach right? It this right way how to control flow or how to write in node.js? Or I am totally on wrong way. Could you please check it and give me some hints, advices etc. Thank you very much for help.