title: Run 3 bash scripts simultaneously then send their return values as JSON in REST API NodeJS
body: <p>I'm trying to run 3 bash scripts depending on what the JSON list sent from the client specifies, then return their outputs to a JSON dict which will be sent to the client again.</p>
  <p>This is the code of the three scripts:</p>
  <pre><code>marc@linux:~ $ cat 1.sh
  sleep 1
  echo -n &quot;a&quot;
  marc@linux:~ $ cat 2.sh
  sleep 1
  echo -n &quot;b&quot;
  marc@linux:~ $ cat 3.sh
  sleep 1
  echo -n &quot;c&quot;
  </code></pre>
  <p>If I executed them synchronously, they might stop the event loop for 3 seconds (undesirable):</p>
  <pre><code>const express = require(&quot;express&quot;);
  const app = express();
  app.use(express.json())
  
  const cp = require(&quot;child_process&quot;);
  
  
  app.get(&quot;/&quot;, (request, response) =&gt; {
      console.log(request.body);
  
      var response_data = {};
      if (request.body.includes(&quot;script1&quot;)) {
          response_data.value1 = cp.execFileSync(&quot;./1.sh&quot;).toString();
      }
      if (request.body.includes(&quot;script2&quot;)) {
          response_data.value2 = cp.execFileSync(&quot;./2.sh&quot;).toString();
      }
      if (request.body.includes(&quot;script3&quot;)) {
          response_data.value3 = cp.execFileSync(&quot;./3.sh&quot;).toString();
      }
  
  
      response.json(response_data);
      response.status(200)
      response.send()
  })
  
  app.listen(8080, () =&gt; {
      console.log(&quot;ready&quot;);
  })
  </code></pre>
  <p>And if I executed them asynchronously, they would return after the response is sent, where the response would be just <code>{}</code></p>
  <p>My intended flow chart is that if I send <code>[&quot;script1&quot;, &quot;script3&quot;]</code>, it should return <code>{&quot;value1&quot;: &quot;a&quot;, &quot;value3&quot;: &quot;c&quot;}</code> when the <code>1.sh</code> and <code>3.sh</code> are done executing, and without blocking the event loop.
  <a href="https://i.stack.imgur.com/IJbdy.png" rel="nofollow noreferrer">Example</a></p>
  <p>How do I implement callbacks/promises in such scenario?</p>