mardi 2 février 2021

how to use a variable that have been declared in if statement to use it back in the else statement

So I am am using cluster and I am bit new to it, I want to get an input which has been declared inside an if statement and use it back in the else statement

Multi_processing = async() => {
  if (cluster. isMaster) {
    const name = await prompt(' input ');// input which want to be re used in else 
    console.log(`Master ${process.pid} is running`);
    for (let i = 0; i <2; i++) {
      cluster.fork();
    
      console.log(name)
    }
    cluster.on('exit', (worker, code, signal) => {
      console.log(`worker ${worker.process.pid} finished`);
    });
  }
    else {
    console.log(name) // name is undefined
    console.log(`Worker ${process.pid} started`);
  }
}
Multi_processing();

If you asking why I didn't put the variable in the else it just do weird things like it continue asking for input and I want to put it in the if statement so the input asked once not of how many times I forked the code

EDIT : just for explantion why i didnt put the input outside the cluster.ismaster here is an image of what happens even the fork number is only 2 enter image description here

Aucun commentaire:

Enregistrer un commentaire