Reputation: 1349
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
Upvotes: 3
Views: 1353
Reputation: 214969
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.
Upvotes: 6