When I tried to run this following code to process multiple chile processes in order, I cannot run final process because before process keeps running. I am newbie of Linux and nodejs.
const childProcess = require('child_process');
function spawn (cmd,option){
return new Promise((resolve)=>{
let p = childProcess.spawn(cmd,option);
p.stdout.setEncoding('utf-8');
p.stdout.on('data', (data)=>{
console.log(data);
});
p.on('exit', (code)=>{
console.log("process id:"+ p.pid + " child EXIT with code ")
// console.log("process id:"+ p.pid + " child exited with code " + code)
resolve();
});
p.stderr.on('data', (data)=>{
console.log("process id:"+ p.pid + " child ERROR " + data)
// console.log(data)
});
})
}
async function nnmain(){
await spawn('ls');
await spawn('mkdir newtemp', {shell: true});
await spawn('df -h', {shell: true, stdio: 'inherit'});
await spawn('mkdir newtemp', {shell: true});
}
nnmain().catch(e=>console.log(e));