rl.question(query[, options], callback)
添加于:v0.3.3
参数
query<string> 要写入output的语句或查询,位于提示符之前。options<Object>signal<AbortSignal> (可选)允许使用AbortController取消question()。
callback<Function> 使用用户输入调用的回调函数,以响应query。
rl.question() 方法通过将 query 写入 output 来显示它,等待在 input 上提供用户输入,然后调用 callback 函数,将提供的输入作为第一个参数传递。
调用时,rl.question() 将恢复已暂停的 input 流。
如果 readline.Interface 是在将 output 设置为 null 或 undefined 的情况下创建的,则不会写入 query。
传递给rl.question() 的callback 函数不遵循接受Error 对象或null 作为第一个参数的典型模式。 callback 以提供的答案作为唯一参数调用。
如果在 rl.close() 之后调用 rl.question() 将引发错误。
示例用法:
rl.question('What is your favorite food? ', (answer) => {
console.log(`Oh, so your favorite food is ${answer}`);
});
使用 AbortController 取消问题。
const ac = new AbortController();
const signal = ac.signal;
rl.question('What is your favorite food? ', { signal }, (answer) => {
console.log(`Oh, so your favorite food is ${answer}`);
});
signal.addEventListener('abort', () => {
console.log('The food question timed out');
}, { once: true });
setTimeout(() => ac.abort(), 10000);
相关用法
- Node.js InterfaceConstructor rl.question(query[, options], callback)用法及代码示例
- Node.js readlinePromises.Interface rl.question(query[, options])用法及代码示例
- Node.js InterfaceConstructor rl.line用法及代码示例
- Node.js InterfaceConstructor rl.write(data[, key])用法及代码示例
- Node.js InterfaceConstructor rl[Symbol.asyncIterator]()用法及代码示例
- Node.js http2.Http2ServerRequest request.url用法及代码示例
- Node.js request.socket用法及代码示例
- Node.js http.ServerResponse response.statusCode用法及代码示例
- Node.js readStream.isRaw用法及代码示例
- Node.js http.ClientRequest request.getHeaders()用法及代码示例
- Node.js http2.Http2ServerRequest request.headers用法及代码示例
- Node.js http.ClientRequest request.setHeader(name, value)用法及代码示例
- Node.js response.writeContinue()用法及代码示例
- Node.js http2.Http2ServerResponse response.removeHeader(name)用法及代码示例
- Node.js response.removeHeader()用法及代码示例
- Node.js http.ServerResponse response.getHeaderNames()用法及代码示例
- Node.js request.writableEnded用法及代码示例
- Node.js http.ClientRequest request.getHeaderNames()用法及代码示例
- Node.js http2.Http2ServerResponse response.hasHeader(name)用法及代码示例
- Node.js http.ClientRequest request.removeHeader(name)用法及代码示例
- Node.js http.ClientRequest request.getHeader(name)用法及代码示例
- Node.js http.ServerResponse response.removeHeader(name)用法及代码示例
- Node.js http.ClientRequest request.reusedSocket用法及代码示例
- Node.js http.ServerResponse response.hasHeader(name)用法及代码示例
- Node.js response.setHeader()用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 rl.question(query[, options], callback)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
