Node.js同步提示

我正在使用Node.js 提示库 ,我有这样的代码:

var fs = require('fs'), prompt = require('prompt'), toCreate = toCreate.toLowerCase(), stats = fs.lstatSync('./' + toCreate); if(stats.isDirectory()){ prompt.start(); var property = { name: 'yesno', message: 'Directory esistente vuoi continuare lo stesso? (y/n)', validator: /y[es]*|n[o]?/, warning: 'Must respond yes or no', default: 'no' }; prompt.get(property, function(err, result) { if(result === 'no'){ console.log('Annullato!'); process.exit(0); } }); } console.log("creating ", toCreate); console.log('\nAll done, exiting'.green.inverse); 

如果显示提示,它似乎不会阻止代码执行,但继续执行,并显示控制台的最后两条消息,而我仍然必须回答这个问题。

有没有办法阻止?

由于Node中的IO没有阻塞,所以你不会find一个简单的方法来实现这种同步。 相反,您应该将代码移到callback中:

  ... prompt.get(property, function (err, result) { if(result === 'no'){ console.log('Annullato!'); process.exit(0); } console.log("creating ", toCreate); console.log('\nAll done, exiting'.green.inverse); }); 

或者提取它并调用提取的函数:

  ... prompt.get(property, function (err, result) { if(result === 'no'){ console.log('Annullato!'); process.exit(0); } else { doCreate(); } }); ... function doCreate() { console.log("creating ", toCreate); console.log('\nAll done, exiting'.green.inverse); } 

用Flatiron的提示库,不幸的是,没有办法阻止代码。 不过,我可能会build议我自己的sync-prompt库。 就像名字所暗示的那样,它可以让你同步提示用户input。

有了它,你只需发出一个函数调用,并取回用户的命令行input:

 var prompt = require('sync-prompt').prompt; var name = prompt('What is your name? '); // User enters "Mike". console.log('Hello, ' + name + '!'); // -> Hello, Mike! var hidden = true; var password = prompt('Password: ', hidden); // User enters a password, but nothing will be written to the screen. 

所以试试看,如果你愿意的话。

请记住: 不要在Web应用程序中使用这个 。 它只能用于命令行应用程序。

Vorpal.js是我刚刚发布的一个库。 它提供了一个交互式提示的同步命令执行,就像你问的那样。 下面的代码将做你在问什么:

 var vorpal = require('vorpal')(); vorpal.command('do sync') .action(function (args) { return 'i have done sync'; }); 

如上所述,一秒钟后提示符会回来(只有在callback被调用之后)。

老问题,我知道,但我只是find了这个完美的工具。 readline-sync为您提供了一种在节点脚本中收集用户input的同步方式。

它使用起来很简单,并且不需要任何依赖(由于gyp问题,我不能使用同步提示)。

从github自述:

 var readlineSync = require('readline-sync'); // Wait for user's response. var userName = readlineSync.question('May I have your name? '); console.log('Hi ' + userName + '!'); 

我不以任何方式参与这个项目,但它只是我的一天,所以我不得不分享。

我遇到了这个线程和所有的解决scheme:

  • 实际上不提供同步提示解决scheme
  • 已经过时,并且不适用于新版本的节点。

为此,我创build了syncprompt 。 使用npm i --save syncprompt安装它 – npm i --save syncprompt ,然后添加:

 var prompt = require('syncprompt'); 

例如,这可以让你做到:

 var name = prompt("Please enter your name? "); 

它也支持提示input密码:

 var topSecretPassword = prompt("Please enter password: ", true); 

从Node.js 8开始,可以使用async / await来执行以下操作:

 const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); function readLineAsync(message) { return new Promise((resolve, reject) => { rl.question(message, (answer) => { resolve(answer); }); }); } // Leverages Node.js' awesome async/await functionality async function demoSynchronousPrompt(expenses) { var promptInput = await readLineAsync("Give me some input >"); console.log("Won't be executed until promptInput is received", promptInput); rl.close(); }