Wait for async process to finish before redirect in koajs

ghstcd

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
}
Farid Nouri Neshat

To wait for an synchronous task/something to be done in koa, you have to yield a function that takes a callback argument. In this case to wait for the child process to be done, you have for the "exit" event to be emitted. Though you can also listen for other child process events like close or end event of stdout. They are emitted before exit.

So in this case yield function (cb) { p.on("exit", cb); } should work which we can reduce it to yield p.on.bind(p, "exit"); using Function::bind

function *task() {
  var p = spawn("process", args);
  p.on("data", function(res) {
    // process data
  });

  p.stdin.write("input");

  yield p.on.bind(p, "exit");

  this.redirect('/'); // wait to execute this
}

You can also use a helper module to help you: co-child-process

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Wait for process to finish before proceeding in Java

Protractor - Wait for async calls to finish before before executing expect

Wait for a child process to finish before starting new child process

Jest: Wait for an async test to finish before running the next one

Wait for async task to finish completion block before returning in app delegate

How to wait for an async connectedCallback to finish before testing the (Lit) Web Component?

wait for async function in loop to finish executing before next iteration

Wait for multiple async calls to finish before React render

Wait partially async function to finish before executing it again

C# Wait for async method to finish before continuing

Wait for async function to finish before app.listen in Express

Wait for async file write to finish before exiting main thread

How to Wait for windows process to finish before opening file in java

Node.JS: how to wait for a process to finish before continuing?

Wait for async task to finish

Wait for an async method to finish

How to wait for all the async to finish?

Wait for an async methods to finish in a for loop

asio - Wait for async operations to finish

How to wait for an async to finish - NodeJs

Wait for all async methods finish

Wait for sound to finish before continuing

How to make a function wait for a async process to complete before commencing?

wait a process to finish in Python script

How to wait for an async call to finish in component's mount callback before unit testing it?

How to correctly wait for async function to finish before executing another function in typescript?

How do I wait for an async function to finish executing before rendering a widget in Flutter

Javascript - wait for async call to finish before returning from function, without the use of callbacks

Node.js - Wait for multiple async calls to finish before continuing in code