然后在我的代码中不起作用(打字稿节点)
then doesn't work in my code (typescript-node)
这是我的代码:
var fs = require('fs');
var util = require('util');
var files = fs.readdirSync('*/path to my folder that contains subfolders*/')
async function getfilenum(){
var files_v_num = []
for(const i in files){
files_v_num[i] = `${i} : ${files[i]}`
}
console.log('files_v_num',files_v_num);
const prompt = require('prompt');
prompt.start();
prompt.get([`number`], function (err, result) {
if (err) {
return onErr(err);
}
console.log('Command-line input received:');
console.log(' Selected ---------------> ' +files_v_num[parseInt(result.number)] );
});
function onErr(err) {
console.log(err);
return 1;
}
}
async function Hello(){
// await getfilenum()
console.log('hello by meeeeeeeeeeeeeeeeeeeeeee')
}
getfilenum().then(Hello)
我想从用户那里获取号码然后打印 'Hello by me'
但它只是像这样打印:
[List of files]
prompt: number: hello by meeeeeeeeeeeeeeeeeeeeeee
如何强制我的代码在收到我的输入后准确地由我打印 hello?
例如像这样:
[List of files]
prompt: number: 3
你好,来自 meeeeeeeeeeeeeeeeeeeeeeeee
由于 prompt 支持 promises (async/await),您最好坚持使用 promises 而不是混合回调和 promises:
const prompt = require('prompt');
async function getInput() {
prompt.start();
return prompt.get(['number']);
}
function showOutput(number) {
console.log('Hello World!');
console.log(number);
}
(async() => {
const number = await getInput();
showOutput();
})();
这是我的代码:
var fs = require('fs');
var util = require('util');
var files = fs.readdirSync('*/path to my folder that contains subfolders*/')
async function getfilenum(){
var files_v_num = []
for(const i in files){
files_v_num[i] = `${i} : ${files[i]}`
}
console.log('files_v_num',files_v_num);
const prompt = require('prompt');
prompt.start();
prompt.get([`number`], function (err, result) {
if (err) {
return onErr(err);
}
console.log('Command-line input received:');
console.log(' Selected ---------------> ' +files_v_num[parseInt(result.number)] );
});
function onErr(err) {
console.log(err);
return 1;
}
}
async function Hello(){
// await getfilenum()
console.log('hello by meeeeeeeeeeeeeeeeeeeeeee')
}
getfilenum().then(Hello)
我想从用户那里获取号码然后打印 'Hello by me' 但它只是像这样打印:
[List of files]
prompt: number: hello by meeeeeeeeeeeeeeeeeeeeeee
如何强制我的代码在收到我的输入后准确地由我打印 hello? 例如像这样:
[List of files]
prompt: number: 3
你好,来自 meeeeeeeeeeeeeeeeeeeeeeeee
由于 prompt 支持 promises (async/await),您最好坚持使用 promises 而不是混合回调和 promises:
const prompt = require('prompt');
async function getInput() {
prompt.start();
return prompt.get(['number']);
}
function showOutput(number) {
console.log('Hello World!');
console.log(number);
}
(async() => {
const number = await getInput();
showOutput();
})();