javascript Node.js读取行,问题等待不工作

rjjhvcjd  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(148)
const readline = require('readline');
const rl = readline.createInterface({input: process.stdin, output: process.stdout});

const answer = await rl.question('What is your favorite food? ');
console.log(`Oh, so your favorite food is ${answer}`);

这是我使用的代码和等待是不工作它说使用高级模块

myzjeezk

myzjeezk1#

除非代码在async function或模块中,否则不能使用await。您可以做的是:

const readline = require('node:readline/promises');
const rl = readline.createInterface({input: process.stdin, output: process.stdout});

async function main(){
    const answer = await rl.question('What is your favorite food? ');
    console.log(`Oh, so your favorite food is ${answer}`);
}

main();

相关问题