Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js how to get data fetch

// way 1
const getData = async (url) => {
  const response = await fetch(url)
  const json = await response.json()

  return json
}

const url = `https://jsonplaceholder.typicode.com/posts?userId=3`

try {
  const data = await getData(url)
  console.log(data)
} catch (error) {
  console.log(error.message)
}

/* ------------ Way 2 --------------- */
const getData = (url) =>
  new Promise((resolve, reject) => {
    fetch(url)
      .then(response => response.json())
      .then(json => resolve(json))
      .catch(error => reject(error))
  })

const url = `https://jsonplaceholder.typicode.com/todos?completed=true&userId=2`

getData(url)
  .then(data => console.log(data))
  .catch(error => console.log(error.message))
Comment

fetch to get data from server

fetch(url).then(function(response) {
  response.text().then(function(dataFromServer) {
    //do stuff
  });
});
Comment

javascript how to fetch data

.catch(function() {

});
Comment

PREVIOUS NEXT
Code Example
Javascript :: remove duplicate array es6 
Javascript :: farewell discord.js 
Javascript :: google recaptcha reload 
Javascript :: Error: contextBridge API can only be used when contextIsolation is enabled 
Javascript :: how to destructure props in react 
Javascript :: how to check if browser is firefox in javascript 
Javascript :: get element by name in jquery 
Javascript :: node.js anonymous function 
Javascript :: api testing app with websocket 
Javascript :: javascript disable div 
Javascript :: mongoose show all indexes 
Javascript :: how to see if user on phone 
Javascript :: javascript replace spaces with br 
Javascript :: remove object from array by value javascript 
Javascript :: jquery form submit 
Javascript :: react native create text file 
Javascript :: javascript promise.all 
Javascript :: pdf to html js 
Javascript :: exit foreach loop js 
Javascript :: You must provide either mongoUrl|clientPromise|client in options 
Javascript :: how to generate random color hexcodes for javascript 
Javascript :: mongoose updateone example 
Javascript :: using regex in javascript 
Javascript :: chrome storage local example 
Javascript :: Regex Chords 
Javascript :: date format date and time in js 
Javascript :: how to allow implicit any in .d.ts 
Javascript :: Converting google document to pdf using Scrips 
Javascript :: discord js send message to specific channel 
Javascript :: is vowel javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =