title: Wait for async process to finish before redirect in koajs
body: <p>I'm currently trying to spawn a child process to handle some POST data with NodeJS (using Koa framework).</p>
  
  <p>Ideally, I would like to wait for the child process to finish before redirecting, but since the child process is asynchronous, the code always redirects first. I've been trying to fix this for a long time and came up with a couple hackish ways to partially solve it, but nothing very clean or usable.</p>
  
  <p>What is the best way to handle this?</p>
  
  <p>Below is the function for my post route (using koa-route middleware).</p>
  
  <pre><code>function *task() {
          var p = spawn("process", args);
          p.on("data", function(res) {
                  // process data
          });
  
          p.stdin.write("input");
  
          this.redirect('/'); // wait to execute this
  }
  </code></pre>