从异步等待函数返回值

时间:2019-05-09 19:33:59

标签: javascript node.js

我尝试从异步await函数返回一个值,但是我不明白为什么我不能这样做。我可能会阅读一些文档,但仍然看不到。有什么可以帮助我的吗?

我使用async-mqtt库,并且我想返回client.on('message');

中包含的值。

我尝试

return await client.on('message', async (top, msg) => {});

但没有

var mqtt_receiver = async (topic) => {
    try {
        client.on('connect', async () => {
            await client.subscribe(topic);
            await client.on('message', async (top, msg) => {
                console.log(top.toString() + " " + msg.toString()); // I want to return this values;
            });
        });
    } catch (error) {
        return(error);
    }
}

var run = async (top) => {
    try {
        var msg = await client.receive(top);
        console.log(msg);
    } catch (error) {
        console.log(error);
    }
};

run("#");

我有“未定义”消息

0 个答案:

没有答案