久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何將 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() 語句不會讓程序等待它們完成,它們只是在 <一個 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 中文字幕高清一区 | 美女一区二区在线观看 | 超碰在线人人 | 一区二区三区在线播放视频 | 国产乱精品一区二区三区 | 日本天天色 | 国产成人免费视频网站视频社区 | 日韩精品免费一区二区在线观看 | 日韩中文字幕2019 | 国产99免费 | 黄视频在线网站 | 国产一区二区在线免费播放 | 一区二区三区视频免费观看 | 日韩电影一区 | 久久日韩粉嫩一区二区三区 | 国产精品我不卡 | 99久9| 国产视频不卡一区 | 国产精品美女一区二区 | 欧美一级毛片免费观看 | 岛国精品| 精品1区2区| 欧美1区 | 成人网址在线观看 | 正在播放亚洲 | a免费视频 | 亚洲欧美日韩电影 | 国产日韩欧美在线 | 欧美日韩国产免费 | 一级国产精品一级国产精品片 | 国产三级日本三级 | 国产一区2区 | 国产91在线观看 | 日韩第一夜 | 日韩高清在线 | 欧美精品一区二区三区在线播放 | 美女天堂 | 激情小说综合网 | 亚洲久视频 | 欧美在线一区二区视频 | 日韩欧美在线不卡 |