我正在尝试从异步函数检索一些数据,但没有成功,代码如下:
links(originalArray = "/views/layouts/footer-links.json"){
let [template, dataset] = async function () {
let html = null,
// Get the post data
response = await fetch(originalArray),
dataset = await response.json(),
template = dataset.footerLinks.map(header=>{
const html = `
<div class="cell medium-3">
<h4 title="${header.type}">${header.text}</h4>
<ul class="menu vertical">
${header.anchors.map(link=>`
<li> ${link.label} </li>
`).join("")}
</ul>
</div>
`;
return html;
}).join(""); ;
return html = {
template: template,
dataset: dataset
};
};
console.log(("In getDataset:::. ",[template, dataset]));
return [template, dataset];
}
这是我遇到的错误
我明白该消息的含义,但我不知道如何绕过它。
最佳答案
就像其他答案和评论所说,
- 您没有调用
异步函数
,而是尝试将其分配给数组解构表达式 - 您没有
等待
它并且 - 它返回一个对象而不是数组
您应该将 links
方法本身设置为 async
。没有办法避免让它返回一个 promise 。
async links(originalArray = "/views/layouts/footer-links.json") {
// Get the post data
const response = await fetch(originalArray);
const dataset = await response.json(),
const template = dataset.footerLinks.map(header => {
const html = `
<div class="cell medium-3">
<h4 title="${header.type}">${header.text}</h4>
<ul class="menu vertical">
${header.anchors.map(link=>`
<li> ${link.label} </li>
`).join("")}
</ul>
</div>
`;
return html;
}).join(""); ;
console.log("In getDataset:::. ", [template, dataset]);
return [template, dataset];
}
关于javascript - 为什么我的异步函数返回 Uncaught TypeError : (intermediate value) is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63046132/