Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

getdata from fetch api into variable

async function getData(url) {
  const response = await fetch(url);

  return response.json();
}

const data = await getData(url);

console.log({ data })
Comment

getdata from fetch api into variable

function getData(url, cb) {
  fetch(url)
    .then(response => response.json())
    .then(result => cb(result));
}

getData(url, (data) => console.log({ data }))
Comment

getdata from fetch api into variable

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;
}
Comment

getdata from fetch api into variable

async function getData(url) {
  const response = await fetch(url);

  return response.json();
}

const data = await getData(url);

console.log({ data })
Comment

getdata from fetch api into variable

function getData(url, cb) {
  fetch(url)
    .then(response => response.json())
    .then(result => cb(result));
}

getData(url, (data) => console.log({ data }))
Comment

getdata from fetch api into variable

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;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript update multiple values of an object 
Javascript :: react-query dependent query 
Javascript :: js delete all cookies 
Javascript :: change class js 
Javascript :: what is javascript runtime 
Javascript :: mongoose populate 
Javascript :: jquery add attribute without value 
Javascript :: javascript input value change 
Javascript :: javascript focus on contenteditable not working 
Javascript :: use of split and join 
Javascript :: js .flat 
Javascript :: alert javascript react native 
Javascript :: react native app crashing on start 
Javascript :: hostlistener 
Javascript :: queryselectors select element whole class 
Javascript :: reactjs get one document from firestore 
Javascript :: alpine js 
Javascript :: react forward ref 
Javascript :: === javascript 
Javascript :: mongodb insertmany 
Javascript :: node cache 
Javascript :: ejs to javascript array 
Javascript :: remove object from array javascript 
Javascript :: expo dependencies 
Javascript :: how to find the lowest number in an array in javascript for specific indexes 
Javascript :: element.js 
Javascript :: add word in string in javascript 
Javascript :: range number in js 
Javascript :: js if text contains lowercase 
Javascript :: react-native-popup-menu 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =