I ' am trying to retrieve some data from an async function without success, here is the code :
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];
}
here is the error i am getting
i understand what the message means but i don't know how to get around it.
I ' am trying to retrieve some data from an async function without success, here is the code :
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];
}
here is the error i am getting
i understand what the message means but i don't know how to get around it.
Share Improve this question asked Jul 23, 2020 at 2:52 Gildas.TamboGildas.Tambo 22.7k7 gold badges53 silver badges79 bronze badges 1-
1
Two problems: you haven't called the
async
fn, and you haven'tawait
ed it (either byawait
or by.then()
) – FZs Commented Jul 23, 2020 at 6:18
3 Answers
Reset to default 2Like the other answers and ments said,
- you're not calling the
async function
but trying to assign it to the array destructuring expression - you're not
await
ing it and - it's returning an object not an array
You should just make the links
method itself async
. There's no way to avoid making it return a 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];
}
It looks like you tried to destructure a function definition into variables template
and dataset
, without actually calling the function to return your expected result.
What you've done is this:
let [template, dataset] = async function () {}
I think what you intended to do was this:
let [template, dataset] = (async function () {}) ()
Or this:
const foo = async function () {} let [template, dataset] = foo()
Edit: Add a response.ok check
if(response.ok) dataset = await response.json()
The reason Chrome plained is because it tried to destructure your function into variables. Since a function unlike an Array, Object among others is not iterable you received the error.
I found two major issues with your code.
- You did not call your IFFE (private scope)
- Your IFFE returns an object however you were destructuring [ template, dataset ] as expecting an array.
async links(originalArray = "/views/layouts/footer-links.json"){
let { template, dataset } = await (async function () { // you have to await your async IFFE since "async" returns promises and "await" awaits and resolves promises
let html = null,
// Get the post data
response = await fetch(originalArray),
dataset = (await response).json(), // I fixed this part for you. You have to await response and then call .json() since response is a Promise not an Object without "await"
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 {
template, // you don't need "template: template"
dataset
};
})(); // call the IFFE
console.log(("In getDataset:::. ",[template, dataset]));
return [template, dataset];
}