如何将 snekfetch 结果放入对象中?
我正在编写一个 discord.js 机器人并尝试使用 Node.js/snekfetch 调用天气 API.问题是我不知道如何将 API 返回的数据放入 javascript 对象中.我想做这样的事情:
I'm writing a discord.js bot and trying to call a weather API using Node.js / snekfetch. The issue is I can't figure out how to just put the data returned from the API into a javascript object. I want to do something like this:
let [country, ...city] = args;
let url = `http://api.openweathermap.org/data/2.5/forecast?q=${city},${country}&units=metric&APPID=${config.weatherID};`;
var weatherObject;
snekfetch.get(url).then(r => weatherObject = r.body);`
但显然这不起作用所以我做错了什么?看起来它应该非常简单,但我就是做不到.由于 snekfetch 似乎没有被广泛使用,我在谷歌上搜索的任何内容都没有帮助,而且我完全无法将我所了解的有关 Promise 的任何信息外推到这种情况.
but obviously this doesn't work so what am I doing wrong? It seems like it should be insanely simple but I just can't do it. Nothing I've googled has helped since snekfetch doesn't seem to be widely used and I've been completely unable to extrapolate anything I've learned about promises to this scenario.
澄清:
snekfetch.get(url).then(r => console.log(r.body));
完全按照预期将对象打印到控制台,而
prints the object exactly as expected to the console, while
snekfetch.get(url).then(r => weatherObject = r.body);
console.log(weatherObject);
打印未定义..then() 语句的工作方式有什么我遗漏的吗?
prints undefined. Is there something I'm missing with how .then() statements work?
推荐答案
.then()
语句不会让程序等待它们完成,它们只是在 <一个 href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" rel="nofollow noreferrer">Promise 他们被解决了.
这意味着您不能可靠地使用已在 Promise 中设置的值,因为 Promise 之后的代码可能会在该 Promise 解析之前执行.
.then()
statements don't make the program wait for them to be completed, they just execute their code after the Promise they're attached to is resolved.
That means that you can't reliably use the value that has been set inside a Promise, because the code after the Promise will probably be executed before that Promise resolves.
您可以决定将其余代码移到该 .then()
语句中,也可以使用 async/await
.
如果您在函数内部,则可以将其声明为 async function
:允许你使用 await
关键字在里面.await
使程序等待 Promise 解决,而不是 Promise,它返回您将在 .then()
函数中使用的值.
这是一个例子:
You can either decide to move the rest of the code inside that .then()
statement or use async/await
.
If you are inside a function, you can declare that as an async function
: that allows you to use the await
keyword inside it. await
makes the program wait for a Promise to resolve, and instead of a Promise it returns the value that you would use in the .then()
function.
Here's an example:
// Instead of using this:
function getResult(args) {
let [country, ...city] = args;
let url = `http://api.openweathermap.org/data/2.5/forecast?q=${city},${country}&units=metric&APPID=${config.weatherID};`;
var weatherObject;
snekfecth.get(url).then(response => {
weatherObject = response.body;
});
return weatherObject; // undefined :(
}
// You could write it like this:
async function getResult(args) {
let [country, ...city] = args;
let url = `http://api.openweathermap.org/data/2.5/forecast?q=${city},${country}&units=metric&APPID=${config.weatherID};`;
let response = await snekfecth.get(url);
var weatherObject = response.body;
return weatherObject; // right value
}
相关文章