getdata from fetch api into variable

Solutions on MaxInterview for getdata from fetch api into variable by the best coders in the world

showing results for - "getdata from fetch api into variable"
Mads
06 Aug 2016
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>
Giulio
09 Sep 2017
1async function getData(url) {
2  const response = await fetch(url);
3
4  return response.json();
5}
6
7const data = await getData(url);
8
9console.log({ data })
Marlene
05 Sep 2016
1function getData(url, cb) {
2  fetch(url)
3    .then(response => response.json())
4    .then(result => cb(result));
5}
6
7getData(url, (data) => console.log({ data }))
Eleonora
01 Jun 2019
1function getFromAPI(url, callback){
2  var obj;
3  fetch(url)
4    .then(res => res.json())
5    .then(data => obj = data)
6    .then(() => callback(obj))
7 }
8
9getFromAPI('https://jsonplaceholder.typicode.com/posts', getData);
10
11function getData(arrOfObjs){
12  var results = "";
13  arrOfObjs.forEach( (x) => {
14    results += "<p> Id: " + x.id + "<ul>"
15    Object.keys(x).forEach( (p) => {
16        results += "<li>" + (p + ": " + x[p]) + "</li>";
17    });
18    results += "</ul> </p> <hr>"
19  })
20  results += "";
21  document.getElementById("myDiv").innerHTML = results;
22}
queries leading to this page
why to assign fetch api to a variablehow can fetch data from api in jsjavascript fetch response to variablefetch data from api and assign to variable javascriptsend data to variable from fetch javascripthow to store fetch data in javascripthow to get api in javascripthow to make api which can fetch datafetch api response into a varhow to get data from an api using fetchassign fetch response to variablehow to select data with fetch api in javascriptget fetch data 2b javascript to variablehow to put the object fetched into a variablehow to a variable from a fetch javascripthow can fetch and display data from api in jsfetch console data to variablehow to return something from fetch api and store into variablejs set variable from fetch json datapull data from api using javascriptjavascript fetch api and save in variableunderstanding how to fetch data from an api in javascripthow to get data from an api in jshow to get data from api in javascriptjavascript get data apipromise to json data javascript in a variablehow to fetch a single data from apifetch api data javascript and return to a variable assign to a variable the result of a response jsonassign fetch data to variablehow to set fetched json to a variableadd fetch response to variablesave fetch response to variableget data from an api in javascriptgetting data through fetchhow to assign data from fetch to a variablehow to fetch data from api in storeapi get method javascriptget data from api jscan i save data from api to a variable and call it later jsstore fetch data in variablehow to use variable from fetch response in jsgetdata function with fetchgetting and using data from an api javascriptgetdata from fetch api into variablefetch turn data into variable jsfetch api save response in variablestore json fetch response in variable javascriptfetch data from apifetch data from server in htmlaccess variable from fetchfetch data through a fi 3dunction javascriptfetch turn data into variableapi to display data in javascriptfetch api response into variablestore fetch api response in variableget data from api and display it in htmlhwo to get a data in jsfetch api set variables inside thenfetch data in get method in apihow to store data from fetch javascriptjavascript fetch result in variablefetch api data store in a variablestoring fetch api in a variableapi to fetch data how to fetch data from apihow to store api fetch data in varmake a fetch js codehow to get data from server using fetching apifhow to fetch api data from websitehow to get data in javascripthow to store api fetch data in a variable javascript 3f 5cfetch api as variablehow to use variable from fetch in jshow to take the fetch result into a variable in javascripthow can i fatch data from rest api to javascripthow to assign data from fetch javascript to a variablefetch data from api with parametersget value for result fetch apijavascript fetched json to constantfetch api from datafetch api with variablehow to assign fetch data to a variable how to include variable in fetch requestfetch get data from api htmlhow to get data from api using fetchstore fetch result in a variablehow to fetch data througbapi ftehcstore data to variable from fetch javascripthow to use variables in fetchfetch data using javascripthow to give a variable fetch dataget request to fetch data from an api on javascriptget data from api in javascriptfetching data from api and assign it to a variable in javascripthow to fetch data from api and store it javascripthow to get fetch api data in js variablehow to save data from fetch in a variableget fetch data in variable javascripthow to convert fetch response to variablefetch data to variablefetch api return value into variable fetch data from an apihow to pass a variable into fetch apistoring fetch result in variablehow to store fetch data in a variable jscan we save fetched json data from fetch function to environment variablehow can i fetch the api in javascript datastore result of fetch in variablehow to use api response in javascriptfetch get response content as variablejavascript fetch data from urlfetch results to a variableconnect fetch data to variablejs store fetch response into a variablehow to store json data in variable from fetch jsstore variable from fetch api javascriptget data from api responsefetch api and set data i variablefetch json and saveget fetch response data into a varaiblejs store fetch result in variablehow to get data by api in htmlhow to use data from api jsfetch data using apijs get data from apihow to get fetch value javascripthow to store data from fetch in javascripthow to fetch javascript data in htmlget info from api javascripthow to store data from fetch api to variablehow to fetch api data and assign to variablesget data using fetch api in javascript with paramshow to store fetched data in jshow to store fetch response in javascript variableget data as variable from fetchjs fetch api json and stor in variablestore fetched json into variableget data from fetch api into variablehow to store data from fetch api in a variablehow to return a variable from a fetch javascriptstore fetch response in variablehow to fetch data from url in javascripthow to store a json fetch inside a variablehow to fetch data from api in javascriptget data by fetch and store it to a variableput fetch data in variablefetch data from a website with jsfetching data through apiget data from api javascriptfetch data from api and declare variablefetch api store json response to variablehow to get api in jsset variable to fetch resulthow to store data into variable after fetchget different variable from response dataassign fetch response to a variablejs set variable to fetch resultget api data javascripthow to get data from an api javascriptfetch data from an api 2cget json data into variable from fetchsimple javascript code to fetch data from apimake a fetch call and get data in a variable jsgetdata from fetch api into variable