【发布时间】:2021-10-17 00:00:27
【问题描述】:
我对承诺不熟悉,因为我试图让一些对象在网页上显示实际值。相反,我只是得到一堆[object Promise]
这是我目前的代码:
\\ This section fetches the api results and puts them into a list/array
async function getitglinkquery(){
var linkresults = await parseitgsearchquery();
console.log(linkresults);
// var linkresultvalues = [];
for (let i = 0; i < linkresults.length; i++) {
var linkresultvalues = fetch(linkresults[i], {
method: "GET",
withCredentials: true,
headers: {
// needed to base64 encode my key with ":x" at the end of the api key then I used that for the authorization header.
"x-api-key": "INSERTAPITHINGYHERE"}})
.then(response => response.json())
}
return linkresultvalues;
}
\\**This section is trying to get the promise and parse the results**
async function parseitglinkquery() {
var queriedresults = await getitglinkquery();
console.log(typeof queriedresults);
// linkresultvalues.push(response);
const output = document.querySelector('span.ms-font-mitglue');
let pre = document.createElement('p');
pre.innerHTML = queriedresults;
pre.style.cssText += 'font-size:24px;font-weight:bold;';
output.appendChild(pre);
}
parseitglinkquery();
}
我尝试了什么
我尝试阅读 firefox promise.resolve 方法,因为这似乎是我正在寻找的:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve 但是我尝试使用他们拥有的静态 promise.resolve 方法示例,但它对我不起作用。像这样:
\\ This section is trying to get the promise and parse the results
async function parseitglinkquery() {
var queriedresults = await Promise.resolve(getitglinkquery());
console.log(queriedresults); \\ This is where my promises are coming out of
}
所以我认为这不是我想要的。我也尝试在这里阅读:https://stackoverflow.com/a/64165144 但我不知道如何使用.then 方法从结果中获取变量。我已经在第一个函数的 api 请求中输入了.then,但我仍然有一个 Promise 列表。
代码图片: enter image description here
结果图片: enter image description here
我尝试了Promise.all(),但它出错并且没有工作,所以我认为这也不是问题所在。我使用 console.log(typeof queriedresults) 仔细检查了我的 queriedresults 变量以确保它是一个对象,它说它是一个对象。
尝试的代码:
async function parseitglinkquery() {
var queriedresults = await Promise.all(getitglinkquery());
}
Error: Uncaught (in promise) TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))
再次失败的图片
我尝试使用:
queriedresults.then(function(result){
console.log(result)
})
基于此处的信息:https://stackoverflow.com/a/29516570/16660683 但这也不起作用,因为我收到错误:
Uncaught (in promise) TypeError: queriedresults.then is not a function
【问题讨论】:
-
看看
Promise.all(),如果你有一系列的承诺,那很可能就是你要找的。另外,尽量避免将 promise 与 await 混用,这会使代码难以推理。 -
你粘贴得不好或者你的代码有一些严重不平衡的
(和{。 -
@MichałSadowski 我发现我的承诺不是一个数组。它看起来像一个,因为我创建了一个名为
linkresultvaluesx的变量并将我的 fetch 放入其中。然后我像这样附加了所有的提取结果:linkresultvalues.push(linkresultvaluesx)。我只假设它不是一个数组,因为当我尝试var querriedresults = await Promise.all(getitglinkquerry());时出现一个错误,上面写着Uncaught (in promise) TypeError: object is not iterable我将尝试使用console.log(typeof querriedresults)查看它的数据类型。很快回来 -
@stsitilegnikcuferasyuguoy 你需要把
return Promise.all(linkresultvalues);insidegetitglinkquerry函数。呼叫不应该改变。 (顺便说一句,我建议将linkresultvalues重命名为linkresultpromises,因为它是一系列承诺) -
@bergi 我很确定你只是一针见血。我改为 [object Object] 但我认为我可以通过在 for 循环中迭代我想要的内容来检索该对象数据。所以我认为这解决了如何从承诺中获得结果的主要问题。如果您对此做出回答,我将给予您信任。另外,我可能会将变量更改为
linkresultpromises。措辞更好,主题更丰富。谢谢
标签: javascript promise