【问题标题】:Use Async functions who call other functions from the same module.exports in Javascript and nodejs在 Javascript 和 nodejs 中使用从同一 module.exports 调用其他函数的异步函数
【发布时间】:2020-04-04 02:16:21
【问题描述】:

我试图制作一个可以使用 NodeJs 读取 JSON 的应用程序。

我所做的事情得到了很好的结果(可能执行起来很慢。) 我想要一个可以查看我的代码的专家意见,因为我感到迷茫和困惑......

其实: killboard.js 为 JSON 使用弯曲

const bent = require('bent');
const getJSON = bent('json');
const config = require('../config.json');

function showKill(kill)
{
    console.log(kill.Killer.Name + " est un meurtrier !");
}

function getKill(killsList) {
    return new Promise((resolve, reject) => {
        killsList.some(function(kill, index) {
            console.log("One kill ...");
            showKill(kill);
        });
    });

}



module.exports = {
    exec: async() =>
    {
        let killsList = await getJSON('https://gameinfo.albiononline.com/api/gameinfo/events?limit=51&offset=0');
        await getKill(killsList);
    }

}

还有我的APP:

const KillBoar = require('./module/killboard');

KillBoar.exec();

效果很好....

但是我该如何整合:showKill 和 getKill 在“module.exports”中的规范。 我的意思是这样的:

module.exports = {
    exec: async() =>
    {
        let killsList = await getJSON('https://gameinfo.albiononline.com/api/gameinfo/events?limit=51&offset=0');
        await getKill(killsList);
    },
    getKill(killsList) {
        return new Promise((resolve, reject) => {
            killsList.some(function(kill, index) {
                console.log("One kill ...");
                showKill(kill);
            });
        });

    },
    showKill(kill)
    {
        console.log(kill.Killer.Name + " est un meurtrier !");
    }

}

因为如果我这样做,我有这个错误:

(node:7392) UnhandledPromiseRejectionWarning: ReferenceError: getKill is not defined
    at Object.exec (C:\Users\Baptiste\Desktop\BotDiscord\KillBoard\module\killboard.js:26:9)
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
(node:7392) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which 
was not handled with .catch(). (rejection id: 1)
(node:7392) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

感谢您花时间回答!

【问题讨论】:

  • 不,它真的不起作用。 getKill 返回的承诺永远不会得到解决。实际上,该函数根本不应该返回一个承诺——它不做任何异步操作。
  • 呃,为什么你的两个sn-ps都定义了两个独立的getKill函数/方法?
  • 复制/过去是错误的
  • 那么正确的做法是什么?

标签: javascript node.js promise async-await


【解决方案1】:

您将它们从函数定义(称为普通函数)更改为 module.exports 对象的方法,因此您现在需要将它们作为方法调用:

module.exports.getKill(killsList);
…
module.exports.showKill(kill);

或者,您也可以选择this.getKill(…)/this.showKill(…),有关差异的详细信息,请参阅here

第三种选择是将它们保留为函数,然后将它们添加到导出中。然后你可以用任何一种方式调用它们:

function showKill(kill) { … }

module.exports = {
    async execc() { … },
    showKill,
};

【讨论】:

  • 我明白了。那么做我想做的最好的方法是什么?最“学术”和干净
  • @BaptisteBauer 最简洁和学术的方法是切换到 ES6 模块声明,并使用命名为 exports。否则,使用this 和方法可能是最常见的。
猜你喜欢
  • 2021-07-07
  • 1970-01-01
  • 2012-02-25
  • 2020-04-20
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
相关资源
最近更新 更多