Last active
April 23, 2022 12:15
-
-
Save ocariocawebdesign/6edf00662ac3f5dfda79826323c330c7 to your computer and use it in GitHub Desktop.
async / await
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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