title: Node.js Control Flow and Callbacks
body: <p>I've been confused on this for a month and searched everything but could not find an answer.</p>
  
  <p>I want to get control of what runs first in the node.js. I know the way node deals with the code is non-blocking. I have the following example:</p>
  
  <pre><code>setTimeOut(function(){console.log("one second passed");}, 1000);
  console.log("HelloWorld");    
  </code></pre>
  
  <p>Here I want to run first one, output "one second passed", and then run the second one. How can I do that? I know setTimeOut is a way to solve this problem but that's not the answer I am looking for. I've tried using callback but not working. I am not sure about if I got the correct understanding of callbacks. Callbacks just mean function parameters to me and I don't think that will help me to solve this problem. </p>
  
  <p>One possible way to solve this problem is to define a function that contains the "error first callback" like the following example:</p>
  
  <pre><code>function print_helloworld_atend(function helloworld(){
      console.log("HelloWorld");
  }){
      setTimeOut(function(){console.log("one second passed");}, 1000);
      helloworld();
  }    
  </code></pre>
  
  <p>Can I define a function with a callback who will know when the previous tasks are done. In the above function, how to make the callback function: helloworld to run after the "setTimeOut" expression?</p>
  
  <p>If there is a structure that can solve my problem, that's my first choice. I am tired of using setTimeOuts.</p>
  
  <p>I would really appreciate if anyone can help. Thanks for reading</p>