0

I have a function wherein I want to do some things with a class object selected by the user. I was thinking, I present them with some options, then after they select it, I use the string to identify the class object in an array of objects like so:

    function askAboutIt() {
  var questions = [{
    type: 'list',
    name: 'theList',
    message: "Message",
    choices: listArray
  }]

  inquirer.prompt(questions).then(answers => {
  var itemInQuestion = (answers['theList']);

   function isPicked(item) { 
    return item.name === itemInQuestion;
}

  var picked = (listArray.find(isPicked)); 
  })

}

Basically inside of some other function I would like to be able to call askAboutIt() and have it return picked. That way I could, for example, console.log(askAboutIt()), or maybe create a variable equal to askAboutIt().someOtherPropertyofmyListArrayClass.

I tried sticking a return in my inquirer function, but it returns as undefined, so then I thought, maybe I could stick an await outside of my console.log, but that's not getting the return either.

So then I tried using the when method from this answer, but then I got returned an error that "when is an unexpected identifier." Where exactly am I supposed to put the when method, or should I use something else entirely?

Z-Man Jones
  • 187
  • 1
  • 12

1 Answers1

0

I figured it out! I was able to do it by inverting my strategy. Instead of calling askAboutIt() in another function, I wrote another function parentFunction(answer) and gave it the parameter answer.

Then, inside askAboutIt, I called that function, like this:

 function askAboutIt() {
  var questions = [{
    type: 'list',
    name: 'theList',
    message: "Message",
    choices: listArray
  }]

  inquirer.prompt(questions).then(answers => {
  var itemInQuestion = (answers['theList']);

   function isPicked(item) { 
    return item.name === itemInQuestion;
}

  var picked = (listArray.find(isPicked)); 
  parentFunction(picked);
  })

}

In this way, I can use the answer of this question in another function.

Z-Man Jones
  • 187
  • 1
  • 12