如何将 snekfetch 结果放入对象中?

How to get snekfetch results into an object?(如何将 snekfetch 结果放入对象中?)
本文介绍了如何将 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
}

这篇关于如何将 snekfetch 结果放入对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)