【发布时间】: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