get data from api in javascript

JavaScript
<script>
function getFromAPI(url, callback){
  var obj;
  fetch(url)
    .then(res => res.json())
    .then(data => obj = data)
    .then(() => callback(obj))
 }

getFromAPI('https://jsonplaceholder.typicode.com/posts', getData);

function getData(arrOfObjs){
  var results = "";
  arrOfObjs.forEach( (x) => {
    results += "<p> Id: " + x.id + "<ul>"
    Object.keys(x).forEach( (p) => {
        results += "<li>" + (p + ": " + x[p]) + "</li>";
    });
    results += "</ul> </p> <hr>"
  })
  results += "";
  document.getElementById("myDiv").innerHTML = results;
}

</script>const GetData = [];
  useEffect(() => {
    fetch(API_URL)
      .then((res) => res.json())
      .then((data) => {
        GetModesData.push(...data);
        setDataState(GetData.map((d) => d.modeName));
      });
  }, []);const GetData = [];
  useEffect(() => {
    fetch(API_URL)
      .then((res) => res.json())
      .then((data) => {
        GetModesData.push(...data);
        setDataState(GetData.map((d) => d.modeName));
      });
  }, []);

Source

Also in JavaScript: