首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从inquirer.js中获取值?

如何从inquirer.js中获取值?
EN

Stack Overflow用户
提问于 2018-03-12 12:18:21
回答 1查看 1.6K关注 0票数 0

我正在使用javascript,yargs,inquirer和superagent构建一个小的CLI应用程序。在inquirer中,我要求用户输入要在我的应用程序中使用的里程选择。我想在我的应用程序中的其他地方使用这个值,但我似乎无法获得返回值。下面是我的最新尝试。如果能帮助我们从selectRange中获得这个返回值,我们将不胜感激。

代码语言:javascript
复制
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}`)
}

Here is a pic of my output, mind the last line

EN

回答 1

Stack Overflow用户

发布于 2018-03-19 12:36:28

您的函数将返回一个Promise,因此您需要使用它:

代码语言:javascript
复制
const surroundingCitiesWeather = (location) => {
    selectRange().then(range => {
        console.log(`Range selected is ${range}`)
    })
}

如果您使用的是最新版本的node,则可以使用async/await使其更加清晰:

代码语言:javascript
复制
const surroundingCitiesWeather = async (location) => {
    const { range } = await selectRange()
    console.log(`Range selected is ${range}`)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49228095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档