我正在使用javascript,yargs,inquirer和superagent构建一个小的CLI应用程序。在inquirer中,我要求用户输入要在我的应用程序中使用的里程选择。我想在我的应用程序中的其他地方使用这个值,但我似乎无法获得返回值。下面是我的最新尝试。如果能帮助我们从selectRange中获得这个返回值,我们将不胜感激。
const selectRange = (result) => {
return inquirer.prompt([{
type: 'checkbox',
message: 'Select the range in miles to search',
name: 'miles',
choices: ['50', '100','150', '200', '250'] ,
validate: (result) => {
if (result.length > 1) {
return 'Error: You must select 1 choice only'
} else {
return true
}
},
filter: input => {
return input
}
}]).then(input => {
return input
})
}
const surroundingCitiesWeather = (location) => {
const range = selectRange()
console.log(`Range selected is ${range}`)
}
发布于 2018-03-19 12:36:28
您的函数将返回一个Promise,因此您需要使用它:
const surroundingCitiesWeather = (location) => {
selectRange().then(range => {
console.log(`Range selected is ${range}`)
})
}如果您使用的是最新版本的node,则可以使用async/await使其更加清晰:
const surroundingCitiesWeather = async (location) => {
const { range } = await selectRange()
console.log(`Range selected is ${range}`)
}https://stackoverflow.com/questions/49228095
复制相似问题