久久久久久久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 服務器時的歡迎消息)
主站蜘蛛池模板: 欧美一级久久 | 成人在线免费观看 | 国产一区二区三区久久久久久久久 | 国产精品一区二区视频 | 欧美一级在线观看 | 午夜精品视频在线观看 | 欧美日韩不卡合集视频 | 欧美专区在线观看 | 中文字幕在线观看 | 在线中文字幕av | 一区在线观看 | 亚洲一区二区精品 | 中文字幕 在线观看 | 黄色在线免费观看视频网站 | 91精品国产综合久久久久久丝袜 | 久久6视频| 亚洲视频在线观看一区二区三区 | 欧美一级做性受免费大片免费 | 免费1区2区3区 | www.成人.com| 黄色一级电影在线观看 | 在线播放中文字幕 | 国产一区二区三区四区 | 欧美区日韩区 | 国产精品一卡二卡三卡 | 日韩福利| 午夜激情免费 | 欧美电影在线观看网站 | 久久精品色欧美aⅴ一区二区 | www.一区二区三区 | 麻豆亚洲 | 国产午夜精品一区二区三区四区 | 一区二区高清 | 国产精品18久久久久久久 | 一区二区三区四区电影视频在线观看 | 91视频在线 | 97视频网站 | av在线播放网 | 99精品国自产在线 | 亚洲播放| 国产激情在线 |