Home > Software design >  then doesn't work in my code (typescript-node)
then doesn't work in my code (typescript-node)

Time:05-27

here is my code :

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)

I want to get number from user then print 'Hello by me' but it's just print like this :

[List of files]
prompt: number:  hello by meeeeeeeeeeeeeeeeeeeeeee

how can I force mycode to print hello by me exactly after get input from me? for example like this :

[List of files]
prompt: number: 3

hello by meeeeeeeeeeeeeeeeeeeeeee

CodePudding user response:

Since prompt supports promises (async/await), you're probably better off just sticking to promises instead of mixing callbacks and 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();
})();
  • Related