I'm currently trying to spawn a child process to handle some POST data with NodeJS (using Koa framework).
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.
What is the best way to handle this?
Below is the function for my post route (using koa-route middleware).
function *task() {
var p = spawn("process", args);
p.on("data", function(res) {
// process data
});
p.stdin.write("input");
this.redirect('/'); // wait to execute this
}