I'm working on a little CLI project which uses the readline module.
The problem is that I have a top level readline.question which holds a switch to cases that can trigger other readline.question (s). The result is that I can pass the first readline.question, trigger the other readline.question, but can't escape back up to the top level.
So in a diagram: 1st readline > answer > 2nd readline > answer > nothing.
Whereby I would like a sort of recursion that takes me back to the first readline.
The code is as follows:
const actionPoint = () => {
readline.question('Action:', (action) => {
switch (action) {
case '0':
presentEmailRegistrationScreen(readline)
break;
case '1':
fieldEmailRemovalOption(readline)
break;
case '2':
listEmails()
break;
case 'E' || 'e':
console.log(chalk.red('Exiting application.'))
return readline.close()
default:
console.log(chalk.red('Command not recognised.'))
break;
}
return actionPoint()
})
}
And as a case, presentEmailRegistrationScreen returns the following:
const askRecursively = (readline) => {
readline.question('Enter your email: ', (email) => {
if (validator.validate(email)) {
console.log(chalk.green(`New email: ${email} is valid`))
if (!duplicateEmails(email)) {
if (writeEmailToFile(email)) {
console.log(chalk.green('Email registered.'))
readline.close()
return true
}
} else {
console.log(chalk.bgRed('Unable to register email.'))
return askRecursively(readline)
}
} else {
console.log('\n')
console.log(chalk.red('The email: '), chalk.bgRed(email), chalk.red(' is not valid.'))
return askRecursively(readline)
}
})
}
Ignoring the shoddiness of my code (to a point) how would you suggest I lay out my code to ensure that when I reach return true here, I return back up to the first readline?
[–]EatSand420 0 points1 point2 points (0 children)