問題描述
我正在編寫一個 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()
語句不會讓程序等待它們完成,它們只是在 <一個 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 結果放入對象中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!