我正在尝试创建readline
with的异步版本promisify
。
以这种方式使用的东西:
import { Cli } from '../services/Cli';
const cli = new Cli();
const main = async () => {
await cli.question('What is your name ? ');
await console.log('\nBYE');
await process.exit(0);
};
这是我的尝试:
import { promisify } from 'util';
import readline from 'readline';
export class Cli {
private cli;
constructor() {
this.cli = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
}
question(text: string) {
return promisify(this.cli.question).call(this.cli, text);
}
}
我受到了我拥有的其他包装器的启发mysql
,它运行良好:
import { promisify } from 'util';
import mysql from 'mysql';
import config from '../../config.test.json';
export class MySQL {
private mySQL;
constructor() {
this.mySQL = mysql.createConnection(config.database);
}
query(sql: string, args?: string | number | [] | {}) {
return promisify(this.mySQL.query).call(this.mySQL, sql, args);
}
}
// use it
this.mySQL = new MySQL();
const users = await this.mySQL.query("SELECT * FROM user");
return users
但是没有返回任何东西。任何的想法?