如何将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似乎没有被广泛使用以来,我一直没有对Google有所帮助,而且我已经完全无法推断出我对这种情况下的承诺所学到的任何东西。

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( )语句不会使程序等待它们完成,它们只是在承诺已解决。

这意味着您不能可靠地使用具有设置在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

如果您在函数内部,您可以声明为 异步功能 :允许您使用 等待 关键字。 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
}