then doesn't work in my code (typescript-node)

Issue

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

Solution

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();
})();

Answered By – Robby Cornelissen

Answer Checked By – Jay B. (AngularFixing Admin)

Leave a Reply

Your email address will not be published.