Skip to content

Instantly share code, notes, and snippets.

@ocariocawebdesign
Last active April 23, 2022 12:15
Show Gist options
  • Save ocariocawebdesign/6edf00662ac3f5dfda79826323c330c7 to your computer and use it in GitHub Desktop.
Save ocariocawebdesign/6edf00662ac3f5dfda79826323c330c7 to your computer and use it in GitHub Desktop.
async / await
//async / await
const result = document.querySelector("#result");
async function fetchPposts(url) {
const response = await fetch(url);
const jsonBody = await response.json();
return jsonBody;
}
const requisicao = fetchPposts("https://meusite.com.br/wp-json/wc/store/products");
requisicao.then((response) => {
console.log(response);
//Pegando os dados do objeto
const dadosProd = {
id: response[0].id,
name: response[0].name,
parent: response[0].parent,
imageSrc: response[0].images[0].src,
};
result.innerHTML = `
<h1>${dadosProd.id}</h1>
<p>${dadosProd.name}</p>
<p>${dadosProd.parent}</p>
<img src="${dadosProd.imageSrc}">`
;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment