How can I build an inquirer prompt array dynamically? i.e. when I'm iterating through items to dynamically add prompt elements.
When I try, the program does not wait for a response. It continues with the rest of the code and exits.
Note: I am using async/await in this example but removing it does not change anything
Working example As a baseline to confirm everything I'm not showing is setup correctly, this code works. The elements are not created in a loop and program waits for user input as expected.
...
const iqElements = [{
type: 'input',
name: 'gitref',
message: 'Git reference:',
default: 'master',
}]
const iq = await inquirer.prompt(iqElements)
Problem Example:
This is the problem. When I build the iqElements array in a for loop, the program does not wait for user input. It continues and exits.
The element
nameis contrived for this simplified example. My actual code sets the name value from unique properties on the elements I'm iterating over.
...
const iqElements = []
for (let i = 0; i < 2; i++) {
iqElements.push({
type: 'input',
name: 'gitref'+i,
message: 'Git reference:',
default: 'master',
})
}
// Confirmed the elements are build before the next line is called
console.log(iqElements)
const iq = await inquirer.prompt(iqElements)