Node.js读取承诺内的行

j9per5c4  于 2022-12-12  发布在  Node.js
关注(0)|答案(5)|浏览(157)

我尝试使用node.js包的readline来获取命令行上的用户输入,并且我想通过promises来传输输入。但是,输入从来没有通过then链。我认为问题可能来自于这样一个事实,即在回调方法中实现了promises,但是我不知道如何解决这个问题。
此问题的示例如下所示:

import rlp = require('readline');

const rl = rlp.createInterface({
  input: process.stdin,
  output: process.stdout
});  

let prom = new Promise(resolve => {
  rl.question('Enter input: ', input => rl.close() && resolve(input));
});

prom
  .then(result => { console.log(result); return prom; })
  .then(result => { console.log(result); return prom; })
  .then(result => console.log(result));

如果在node.js中运行,这个问题只会出现一次,输入完后程序就停止了。我希望它等到第一个输入完成后,它会打印这个输入并询问下一个输入。
提前感谢!

nnsrf1az

nnsrf1az1#

一旦你的承诺得到了解决,就没有必要再等待了。我还把rl.close()调用移到了最后,因为它只需要被调用一次。

const rlp = require('readline');

const rl = rlp.createInterface({
  input: process.stdin,
  output: process.stdout
});

function ask() {
  return new Promise(resolve => {
    rl.question('Enter input: ', input => resolve(input));
  });
}

ask()
  .then(result => { console.log(result); return ask(); })
  .then(result => { console.log(result); return ask(); })
  .then(result => { console.log(result); rl.close() });
jmo0nnb3

jmo0nnb32#

这是这个问题的答案,我不值得称赞。

// Function
function Ask(query) {
  const readline = require("readline").createInterface({
    input: process.stdin,
    output: process.stdout
  })

  return  new Promise(resolve => readline.question(query, ans => {
  readline.close();
  resolve(ans);
}))
}

 
// example useage
async function main() {

 var name = await Ask("whats you name")
 console.log(`nice to meet you ${name}`)

 var age = await Ask("How old are you?")
 console.log(`Wow what a fantastic age, imagine just being ${age}`)
}

main()
qyuhtwio

qyuhtwio3#

const readline = require('readline');

const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const ask = (query) => new Promise((resolve) => rl.question(query, resolve));

ask('A: ').then(async (a) => {
  const b = await ask('B: ');
  const c = await ask('B: ');

  console.log(a, b, c);

  rl.close();
});

rl.on('close', () => process.exit(0));
cvxl0en2

cvxl0en24#

第一个字符:

import * as readline from 'node:readline/promises'
import { stdin as input, stdout as output } from 'process'

const rl = readline.createInterface({input, output})

const answer = await rl.question('What do you think of Node.js? ')

console.log(`Thank you for your valuable feedback: ${answer}`)

rl.close()

https://nodejs.org/api/readline.html#readline

zujrkrfu

zujrkrfu5#

节点提示.mjs

import { createInterface as createQuestionInterface } from 'readline';

const rl = createQuestionInterface({
  input: process.stdin,
  output: process.stdout
});

function questionLine(multiline, resolve, i, input, rl) {
  if (!multiline) {
    resolve(i);
  } else {
    if (input && !i) {
      resolve(input);
    } else {
      return input + i + "\r\n";
    }
  }
  return input;
}

function promptMultiLine(questionText) { // This is async by returning promise
  return prompt(questionText, true);
}

async function prompt(questionText, multiline = false) {
  return await (new Promise((resolve, reject) => {
    let input = '';
    rl.question(`${questionText}: `, (i) => {
      input = questionLine(multiline, resolve, i, input, rl);
    });
    rl.on('line', (i) => {
      input = questionLine(multiline, resolve, i, input, rl);
    });
  }));
}

async function run() {
  const question = prompt("please enter response [enter to complete]");
  console.log(question);
  const questionMultiLine = promptMultiLine("please enter response [enter text and enter twice]");
  console.log(questionMultiLine);
}

run();

相关问题