title: Node.js running two functions in parallel
body: <p>I need to run two tasks in parallel in Node.js like how we spawn two threads in Java. I looked though all of the documentation related to promises and async/await.</p>
  <p>I went through the following threads:</p>
  <ol>
  <li><a href="https://stackoverflow.com/questions/14035015/parallel-function-calls-in-node-js">Parallel function calls in Node.js</a></li>
  <li><a href="https://stackoverflow.com/questions/42772401/parallel-operations-with-promise-all">Parallel operations with Promise.all?</a></li>
  </ol>
  <p>Even if I use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all" rel="nofollow noreferrer">Promise.all</a>, the code is getting executed sequentially.</p>
  <p>I couldn't find a way to do so. I see that <a href="https://nodejs.org/api/http.html#httpgetoptions-callback" rel="nofollow noreferrer">HTTP.get</a> executes in the background, and once the results are available, it returns the response in callback. Until that happens, my program can perform other tasks. Can I do something like this to accomplish parallelism in my own code?</p>
  <p>I have the following code. I want to make sure that <code>func1</code> and <code>func2</code> run in parallel.</p>
  <p>I am expecting an output in the following order:</p>
  <pre><code>calling func1
  calling func2
  called all functions
  promise 1 done!
  promise 2 done!
  </code></pre>
  <p>This is my code</p>
  <pre><code>function func1() {
  
    let promise = new Promise((resolve, reject) =&gt; {
      try {
      for(var q=0;q&lt;10000;q++) {
          console.log(q);
      }
      resolve(&quot;promise 1 done!&quot;)
      } catch(e) {
      reject(e);
      }
    });
    return promise;
  }
  
  function func2() {
  
    let promise = new Promise((resolve, reject) =&gt; {
      try {
      for(var r=0;r&lt;10000;r++) {
          console.log(r);
      }
      resolve(&quot;promise 2 done!&quot;)
      } catch(e) {
      reject(e);
      }
    });
    return promise;
  }
  
  
  function func() {
      console.log(&quot;calling func1&quot;);
      var promise1 = func1();
  
      console.log(&quot;calling func2&quot;);
      var promise2 = func2();
  
      console.log(&quot;called all functions&quot;);
      //do some other task
  
      promise1
  .then(result =&gt; { console.log(&quot;Promise 1 : &quot;+result); })
  .catch(error =&gt; { console.log(&quot;Promise 1 err : &quot;+error ); });
  
      promise2
  .then(result =&gt; { console.log(&quot;Promise 2 : &quot;+result); })
  .catch(error =&gt; { console.log(&quot;Promise 2 err : &quot;+error ); });
  }
  
  func();
                                                                                                                                                                                                      
  </code></pre>
  <p>In short, how can I make the two for loops execute in parallel? Is it possible in Node JS or am I missing something?</p>