title: Node.Js function runs twice when called once on promise return
body: <p>I am attempting to create as many Google Slide slides as needed based on user input. In theory, my code should work if I can fix this one problem. The problem is, I can't. When I run my code, it stops because the createSlide() function runs twice on one call and creates two slides with the same Id. Poking around on the web, I've only found things about Node.Js input from a browser. First time through it runs ok, it does what it's supposed to, but when slide two (id: slide_1) is created it makes the slide, doubles the text such as:</p>
  
  <pre><code>Hi
  bye
  </code></pre>
  
  <p>Would create</p>
  
  <pre><code>Hi
  bye
  Hi
  bye
  </code></pre>
  
  <p>Then stop due to the fact that there cannot be two Ids that are the same. Here is my code (I've deleted the requests since the slide creation works fine): </p>
  
  <pre><code>function askYOrN(auth){
  
      console.log("HELLO");
  
      const r4 = readline.createInterface({
          input: process.stdin,
          output: process.stdout
      });
  
      r4.question("Do you want to add a slide [Y/N]? ", function(yesOrNo){
          yesOrNoLog = yesOrNo;
          r4.close();
          fAskForNewCounter = fAskForNewCounter + 1;
          askForNew(auth);
      });
  }
  
  //ASKS IF ANOTHER SLIDE IS NEEDED
  function askForNew(auth){
  
      //READLINE
      const r5 = readline.createInterface({
          input: process.stdin,
          output: process.stdout, 
          terminal: false
      });
  
      //CLEARS txt
      txtArray = [];
  
      //CHECKS THE ANSWER AND DOES ACCORDINGLY
      if (yesOrNoLog == "yes" || yesOrNoLog == "y"){
  
          //ASKS FOR TEXT AND ASSIGNS IT TO THE VARIABLE
          r5.prompt();
          console.log("Text:\n");
          r5.on('line', function (textIn) {
              if (textIn == ".exit" || textIn == ".e" || textIn == ".next" || textIn == ".n"){
                  createSlide(auth);
              }else{
                  textArray.push(textIn);
                  textArray.push('\n');
              }
          }); 
      }else if (yesOrNoLog == "no" || yesOrNoLog == "n"){
  
          //CREATES TITLE SLIDE
          createTitleSlide(auth);
      }else{
  
          //ASKS FOR VALID ANSWER
          if (fAskForNewCounter &gt;= 0){
              console.log("Enter a valid answer");
              askYOrN(auth);
          }
      }
  }
  
  //DECLARATION FOR THE NUMBER COUNTER [ num ]
  var num = 0;
  
  function createSlide(auth) {
  
      //AUTHENTICATION
      const slides = google.slides({version: 'v1', auth});
  
      //CHANGES txtArray TO STRING
      var txt = txtArray.join('');
  
      //CHANGING VARS
      var slideId = 'slide_' + num;
      var pageId = slideId;
  
      var textId = 'text_box_' + num;
      var elementId = textId;
  
      var iIndex = num;
  
      //SLIDE NUMBER
      var sNum = num + 1;
  
  
          console.log(pageId);
  
      //ALL REQUESTS GO IN requests
           var requests = [{   
          }];
  
      //BATCH UPDATE
      return slides.presentations.batchUpdate({
          presentationId,
          resource: {
              requests,
          },
      }, (err, res) =&gt; {
          if (err) {
              error(err);
          }else{
              console.log("Slide #" + sNum + " has been created");
  
              //INCREASES COUNTER BY 1
              num = num + 1;
  
              //ASKS IF A NEW SLIDE WANTS TO BE CREATED
              var delay = 30;
              var lastClick = 0;
  
              if (lastClick &gt;= (Date.now() - delay))
              return;
              lastClick = Date.now();
  
              yesOrNoLog = "";
              askYOrNo(auth);
          }
      });
  }
  </code></pre>
  
  <p>Thanks in advance for your help!</p>
  
  <p><strong>EDIT</strong><br>
  I have done some more work and have found that in my code r5.on is run twice. I tried deleting the array.push("\n"). but this does not fix the problem. The array outputs as</p>
  
  <pre><code>[hi, bye, hi, bye]
  </code></pre>
  
  <p>This is my new code once again I have deleted the requests:</p>
  
  <pre><code>//ASKS IF YOU WANT A NEW SLIDE
  function askYOrN(auth){
  
      console.log("askYOrN");
  
      const r4 = readline.createInterface({
          input: process.stdin,
          output: process.stdout,
          terminal: false
      });
  
      r4.question("Do you want to add a slide [Y/N]? ", function(yesOrNo){
          console.log("aksYOrN question");
          yesOrNoLog = yesOrNo;
          r4.close();
          yOrNCheckCounter = yOrNCheckCounter + 1;
          askYOrNCheck(auth);
      });
  }
  
  //ASKS IF ANOTHER SLIDE IS NEEDED
  function askYOrNCheck(auth){
      console.log("askYOrNCheck begins");
      //READLINE
      const r5 = readline.createInterface({
          input: process.stdin,
          output: process.stdout, 
          terminal: false
      });
  
      //CLEARS TXT
      txtArray = [];
  
      //CHECKS THE ANSWER AND DOES ACCORDINGLY
      if (yesOrNoLog == "yes" || yesOrNoLog == "y"){
  
          //ASKS FOR TXT AND ASSIGNS IT TO THE VARIABLE
          console.log("prompt opens");
          r5.prompt();
          console.log("Text:");
          r5.on('line', function (textIn) {
              console.log("r5.on");
              if (textIn == ".exit" || textIn == ".e" || textIn == ".next" || textIn == ".n"){
                  console.log("if next");
                  createSlide(auth);
              }else{
                  console.log(txtArray);
                  console.log("about to push text");
                  txtArray.push(textIn);
                  console.log(txtArray);
              }
          }); 
      }else if (yesOrNoLog == "no" || yesOrNoLog == "n"){
  
          //CREATES TITLE SLIDE
          createTitleSlide(auth);
      }else{
  
          //ASKS FOR VALID ANSWER
          if (yOrNCheckCounter &gt;= 0){
              console.log("Enter a valid answer");
              askYOrN(auth);
          }
      }
  }
  
  //DECLARATION FOR THE NUMBER COUNTER [ num ]
  var num = 0;
  
  function createSlide(auth) {
  
      //AUTHENTICATION
      const slides = google.slides({version: 'v1', auth});
  
      //CHANGES txtArray TO STRING
      var text = txtArray.join('\n');
  
      //CHANGING VARS
      var slideId = 'slide_' + num;
      var pageId = slideId;
  
      var textId = 'text_box_' + num;
      var elementId = textId;
  
      var iIndex = num;
  
      //SLIDE NUMBER
      var sNum = num + 1;
  
      console.log(pageId);
  
      //ALL REQUESTS GO IN [ requests ]
              var requests = [];
  
      //BATCH UPDATE
      return slides.presentations.batchUpdate({
          presentationId,
          resource: {
              requests,
          },
      }, (err, res) =&gt; {
          if (err) return error(err);
          console.log("Slide #" + sNum + " has been created");
  
          //INCREASES COUNTER BY 1
          num = num + 1;
  
          //ASKS IF A NEW SLIDE WANTS TO BE CREATED
          askYOrN(auth);
      });
  }
  </code></pre>
  
  <p><em>Note:</em>
  I renamed askForNew() to askYOrNoCheck().</p>
  
  <p>Once again thanks for your help.</p>