【问题标题】:JS Asynchronous undefinedJS 异步未定义
【发布时间】:2018-03-02 16:49:30
【问题描述】:

我已经阅读了以下堆栈溢出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

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

【问题讨论】:

  • erm return await post ?不...只是return post(我假设getMsg1getMsg2是同步的)
  • 读取 documentation for Bluebird.delay ... 返回一个承诺,该承诺将在给定 ms 毫秒后以值(或未定义)解决) - 好吧,因为您没有传入 .delay 的值,它实际上 确实 解析为未定义 - 因此,您的 if 始终为 false
  • @JaromandaX 我指定了延迟,2000,是的,它们是同步的
  • 是的,你已经指定了延迟,但没有第二个参数......因此,delayValue 将始终为undefined - 它在文档中这么说......为什么2秒延迟?你认为delayValue 可能是什么?对或错?是什么让它成真?是什么让它错了?
  • 或者,更简单地说.. 2 秒延迟的目的是什么?

标签: javascript node.js asynchronous


【解决方案1】:

我只是暂时离开这里

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

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

        geocoder = NodeGeocoder(geocodeOptions);
        let msg = await geocoder.reverse({lat, lon})
        .then(([res]) => "*" + res.city + "*")

        return getMsg1() + '\n' + getMsg2() + '\n' + EncId;
    } catch (error) {
        console.log(error)
    }
}

好的 - 让我解释一下您的代码的一些(相关)更改

由于NodeGeocoder返回一个Promise,我们可以等待,但是你需要在.then中返回一些东西,否则你会得到undefined

awaiting geocoder.reverse 意味着不需要(hacky)暂停 2 秒,所以,它已经消失了。除了

let x = await Bluebird.delay(2000)

将始终导致 x 未定义 - 你会想要的

let x = await Bluebird.delay(2000, true)

至少,如果您希望 x 是真实的 - 但由于这只是延迟,因此真的没有理由测试 Bluebird.delay 的结果 - 因为我们已经知道了


不太相关的变化:

我做了一些小的 (ES2015+) 更改,例如速记箭头符号,以及 ([res]) => res.city 而不是 res => res[0]['city'] 只是为了使代码更整洁

还有

let [lat, lon] = await getDetails(messageContent);

而不是

let EncId = await getDetails(messageContent)

并以 EndId[0] 身份访问 lat 和以 EndId[1] 身份访问 lon - 也意味着

{ lat: EncId[0], lon: EncId[1] }

只是

{ lat, lon }

【讨论】:

  • 不知道为什么这被否决了,也许是缺乏解释 - 当我发布答案时,“现实世界”™需要我关注几个小时
猜你喜欢
  • 2018-08-23
  • 2023-04-08
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
  • 2015-08-21
  • 2021-12-21
  • 2016-04-03
  • 1970-01-01
相关资源
最近更新 更多