3

I want to make my main process wait on some output from an exec.

I want to force the program to perform the steps in sequence, waiting for each step before going onto the next step. I feel this should be easy, yet I cannot see how to do it.

With other languages it is the default to wait on things, in Perl for example the following 5 lines would do exactly what I want.

print("START")
print(`sleep 3; echo 1`)
print(`sleep 2; echo 2`)
print(`sleep 1; echo 3`)
print("END")

How to do it in Node JavaScript?

Here is my code so far

const util = require('util');
const exec = require('child_process').exec;

function sleepy1() {
  exec("sleep 3; echo 1", function( stderr, stdout ) {
    console.log( stdout.trim() )
  })
}

function sleepy2() {
  exec("sleep 2; echo 2", function( stderr, stdout ) {
    console.log( stdout.trim() )
  })
}

function sleepy3() {
  exec("sleep 1; echo 3", function( stderr, stdout ) {
    console.log( stdout.trim() )
  })
}

console.log("START")
sleepy1()
sleepy2()
sleepy3()
console.log("END")

Here is the output from the code:

$ node make_it_wait.js 
START
END
3
2
1
1
  • This is a Duplicate, please search StackOverflow before asking a question. Commented Jun 16, 2019 at 16:29

1 Answer 1

6

You can promisify exec and await until each command finishes:

const util = require('util');
const exec = require('child_process').exec;

const execa = util.promisify(exec);

function sleepy1() {
  return execa("sleep 3; echo 1")
}

function sleepy2() {
  return execa("sleep 2; echo 2")
}

function sleepy3() {
  return execa("sleep 1; echo 3")
}

async function main() {
    console.log((await sleepy1()).stdout)
    console.log((await sleepy2()).stdout)
    console.log((await sleepy3()).stdout)
}


console.log("START");
main().then(() => console.log("END"))

As pointed out in another answer, there's also the sync variant of exec, execSync. If what you do is a script or another one-pass program, you might find that easier to use. In a server/multiprocess environment it's better to use async/await.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.