title: How do I invoke inquirer.js menu in a loop using Promises?
body: <p>I wrote a simple Node.js program with a nice menu system facilitated by <a href="https://github.com/SBoudrias/Inquirer.js/" rel="nofollow noreferrer">inquirer.js</a>. However, after selecting an option in the menu and completing some action, the program exits. I need the menu to show again, until I select the Exit [last] option in the menu. I would like to do this using Promise, <em>instead</em> of async/await.</p>
  
  <p>I tried using a function to show the menu and called that function within a forever loop (E.g. <code>while (true) { ... }</code>), but that made the program unusable. I changed that to a for-loop just to observe the problem. Below is the simple program and the resulting output.</p>
  
  <p><strong><code>PROGRAM</code></strong></p>
  
  <pre><code>"use strict";
  
  const inquirer = require('inquirer');
  const util = require('util')
  
  // Clear the screen
  process.stdout.write("\u001b[2J\u001b[0;0H");
  
  const showMenu = () =&gt; {
    const questions = [
      {
        type: "list",
        name: "action",
        message: "What do you want to do?",
        choices: [
          { name: "action 1", value: "Action1" },
          { name: "action 2", value: "Action2" },
          { name: "Exit program", value: "quit"}
        ]
      }
    ];
    return inquirer.prompt(questions);
  };
  
  const main = () =&gt; {
    for (let count = 0; count &lt; 3; count++) {
      showMenu()
      .then(answers =&gt; {
        if (answers.action === 'Action1') {
          return Promise.resolve('hello world');
        }
        else if (answers.action === 'Action2') {
          return new Promise((resolve, reject) =&gt; {
            inquirer
              .prompt([
                {
                  type: 'input',
                  name: 'secretCode',
                  message: "Enter a secret code:"
                }
              ])
              .then(answers =&gt; {
                resolve(answers);
              })
          });
        }
        else {
          console.log('Exiting program.')
          process.exit(0);
        }
      })
      .then((data) =&gt; { console.log(util.inspect(data, { showHidden: false, depth: null })); })
      .catch((error, response) =&gt; {
        console.error('Error:', error);
      });
    }
  }
  
  main()
  </code></pre>
  
  <p><strong><code>OUTPUT</code></strong></p>
  
  <pre><code>? What do you want to do? (Use arrow keys)
  ❯ action 1
    action 2
    Exit program ? What do you want to do? (Use arrow keys)
  ❯ action 1
    action 2
    Exit program ? What do you want to do? (Use arrow keys)
  ❯ action 1
    action 2
    Exit program (node:983) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 keypress listeners added to [ReadStream]. Use emitter.setMaxListeners() to increase limit
  </code></pre>
  
  <p>How can I block after the first call to generate the menu, wait for an option to be selected and the corresponding action to complete, and then cycle back to the next iteration of showing the menu?</p>