0

I've got a function which uses inquirer to ask a series of questions and then calls a function in the inquirer.prompt(q).then() function with the answers. This works perfectly well but I'm trying to do some refactoring of my app and I want to be able to make my function return the set of answers to be called elsewhere. However, the code I have seems to return nothing at all. I thought I'd followed the examples correctly but apparently not. Can someone shed some light on the issue, please?

Here is the function:

const inquirer = require('inquirer')
const questions = require('../data/questions')

// Ask the user the predefined questions
module.exports = questionTime = async () => await inquirer.prompt(questions)

I've also tried to write it out the long-form way to see if that helped but to no avail:

module.exports = questionTime = async function () {
  const answers = await inquirer.prompt(questions)
  return answers
}

If I console.log answers instead of returning then I can see the output but if I import the function to another file and console.log that I get nothing:

e.g. console.log(questionTime())

Alex Foxleigh
  • 1,784
  • 2
  • 21
  • 47
  • where is `questions` defined? – trincot Feb 16 '20 at 16:27
  • I've updated the question to include the full file. – Alex Foxleigh Feb 16 '20 at 16:28
  • 1
    OK, so `questionTime()` will return a promise. You will have to await it to get the answers. There is no magical way to be able to access answers *now*, when they are only available in the *future*. Wrapping things in yet another `async` function is not going to change this. – trincot Feb 16 '20 at 16:29

0 Answers0