1<script>
2function getFromAPI(url, callback){
3 var obj;
4 fetch(url)
5 .then(res => res.json())
6 .then(data => obj = data)
7 .then(() => callback(obj))
8 }
9
10getFromAPI('https://jsonplaceholder.typicode.com/posts', getData);
11
12function getData(arrOfObjs){
13 var results = "";
14 arrOfObjs.forEach( (x) => {
15 results += "<p> Id: " + x.id + "<ul>"
16 Object.keys(x).forEach( (p) => {
17 results += "<li>" + (p + ": " + x[p]) + "</li>";
18 });
19 results += "</ul> </p> <hr>"
20 })
21 results += "";
22 document.getElementById("myDiv").innerHTML = results;
23}
24
25</script>