const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('请输入文件所在路径:', (path) => {
let file_path = path;
rl.question('请输入新的文件名:', (name) => {
let file_name = name;
console.log(file_path);
console.log(file_name);
rl.close();
});
});
有没有办法直接定义全局变量,然后用 readline 读入变量?
你可以试试用
util
库的promisify
,require('util').promisify
或者bluebird
的promisifyAll
方法,看看能不能把你question()
转成promise
的,如果可以的话,你就可以使用Async/Await
语法了,就能避免嵌套回调了。