JS异步未定义

时间:2018-03-02 04:41:04

标签: javascript node.js asynchronous

我已阅读以下堆栈溢出postthis one,但我仍无法找到解决问题的方法。

我在utility.js文件中定义了函数,如下所示:

let geocodeOptions = {
    provider: 'google',
    httpAdapter: 'https',
    apiKey: 'GMAPKEY_HERE',
    formatter: null
};
async function postInformation(message, bot) {
    try {
        let messageContent = message.content;        
        let EncId = await getDetails(messageContent), msg

const NodeGeocoder = require('node-geocoder');

geocoder = NodeGeocoder(geocodeOptions);
        geocoder.reverse({ lat: EncId[0], lon: EncId[1] }).then(res => {
            msg = "*" + res[0]['city'] + "*";
        })

        let post;

        let delayValue = await Bluebird.delay(2000)
        if (delayValue) {
            post = getMsg1() + '\n' + getMsg2() + '\n' + EncId;
            return await post
        }

    } catch (error) {
        console.log(error)
    }
}

在主文件中,我按如下方式调用它:

const util = require('./utility')
util.postInformation(message, client).then(value => {
     console.log("value from main file")
     console.log(value) // Always prints undefined
})

我也尝试过:

let value = await util.postInformation(message, client);
console.log("value from main file")
console.log(value) // Always prints undefined

不是重定向到其他帖子,而是提供这个问题的解决方案,我在这里犯的错误在哪里。

0 个答案:

没有答案
相关问题